diff options
| author | Anastasia Karpinska <akarpinska@griddynamics.com> | 2013-09-04 13:05:57 +0400 |
|---|---|---|
| committer | Ivan A. Melnikov <imelnikov@griddynamics.com> | 2013-09-04 19:14:10 +0400 |
| commit | 6ee4d32fc25a7343ef4802f8390e76ef608106b9 (patch) | |
| tree | d1931544f347e5724108ff074468ef9f9ccb22a0 /taskflow/utils/threading_utils.py | |
| parent | a91dd6b0e4e31132d1fcf0f9f6c2f64b599bfda0 (diff) | |
| download | taskflow-6ee4d32fc25a7343ef4802f8390e76ef608106b9.tar.gz | |
MultiThreaded engine and parallel action
MultiThreaded engine was implemented to execute tasks in parallel:
- added parallel action that executes and reverts tasks in parallel;
- added thread-safe storage.
Change-Id: I4a1f78c95ae5d38660bd32ce21d2b3fb1b2af8ad
Diffstat (limited to 'taskflow/utils/threading_utils.py')
| -rw-r--r-- | taskflow/utils/threading_utils.py | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/taskflow/utils/threading_utils.py b/taskflow/utils/threading_utils.py index 05c5e35..127ed71 100644 --- a/taskflow/utils/threading_utils.py +++ b/taskflow/utils/threading_utils.py @@ -20,6 +20,7 @@ import logging import threading import threading2 import time +import types LOG = logging.getLogger(__name__) @@ -144,3 +145,21 @@ class ThreadGroupExecutor(object): if not self._threads: return return self._group.join(timeout) + + +class ThreadSafeMeta(type): + """Metaclass that adds locking to all pubic methods of a class""" + + def __new__(cls, name, bases, attrs): + from taskflow import decorators + for attr_name, attr_value in attrs.iteritems(): + if isinstance(attr_value, types.FunctionType): + if attr_name[0] != '_': + attrs[attr_name] = decorators.locked(attr_value) + return super(ThreadSafeMeta, cls).__new__(cls, name, bases, attrs) + + def __call__(cls, *args, **kwargs): + instance = super(ThreadSafeMeta, cls).__call__(*args, **kwargs) + if not hasattr(instance, '_lock'): + instance._lock = threading.RLock() + return instance |
