summaryrefslogtreecommitdiff
path: root/taskflow/utils/threading_utils.py
diff options
context:
space:
mode:
authorJoshua Harlow <harlowja@gmail.com>2013-10-06 21:38:43 +0000
committerJoshua Harlow <harlowja@yahoo-inc.com>2013-10-07 15:43:21 -0700
commite70032d0d9749a48bf56f14b7b677fb5d92ff332 (patch)
treec2000d41956b7aa839248e428bab3ce06a5d0d55 /taskflow/utils/threading_utils.py
parent8750840ac872cb1bf983be5cbad3d0448e4f3ee6 (diff)
downloadtaskflow-e70032d0d9749a48bf56f14b7b677fb5d92ff332.tar.gz
Remove decorators and move to utils
In order to avoid the circular import in threading utils move the decorators functionality to utils/misc and move the locking functionality to utils/lock_utils and then use these functions from the threading util (and elsewhere). Fixes bug: 1236080 Change-Id: I9e71c2ba15782cbb6dd5ab7e1264b77ed47bc29e
Diffstat (limited to 'taskflow/utils/threading_utils.py')
-rw-r--r--taskflow/utils/threading_utils.py44
1 files changed, 2 insertions, 42 deletions
diff --git a/taskflow/utils/threading_utils.py b/taskflow/utils/threading_utils.py
index 5c6e0bc..dc24353 100644
--- a/taskflow/utils/threading_utils.py
+++ b/taskflow/utils/threading_utils.py
@@ -22,6 +22,7 @@ import threading
import time
import types
+from taskflow.utils import lock_utils
LOG = logging.getLogger(__name__)
@@ -57,46 +58,6 @@ def get_optimal_thread_count():
return 2
-class MultiLock(object):
- """A class which can attempt to obtain many locks at once and release
- said locks when exiting.
-
- Useful as a context manager around many locks (instead of having to nest
- said individual context managers).
- """
-
- def __init__(self, locks):
- assert len(locks) > 0, "Zero locks requested"
- self._locks = locks
- self._locked = [False] * len(locks)
-
- def __enter__(self):
-
- def is_locked(lock):
- # NOTE(harlowja): the threading2 lock doesn't seem to have this
- # attribute, so thats why we are checking it existing first.
- if hasattr(lock, 'locked'):
- return lock.locked()
- return False
-
- for i in xrange(0, len(self._locked)):
- if self._locked[i] or is_locked(self._locks[i]):
- raise threading.ThreadError("Lock %s not previously released"
- % (i + 1))
- self._locked[i] = False
- for (i, lock) in enumerate(self._locks):
- self._locked[i] = lock.acquire()
-
- def __exit__(self, type, value, traceback):
- for (i, locked) in enumerate(self._locked):
- try:
- if locked:
- self._locks[i].release()
- self._locked[i] = False
- except threading.ThreadError:
- LOG.exception("Unable to release lock %s", i + 1)
-
-
class CountDownLatch(object):
"""Similar in concept to the java count down latch."""
@@ -137,11 +98,10 @@ 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)
+ attrs[attr_name] = lock_utils.locked(attr_value)
return super(ThreadSafeMeta, cls).__new__(cls, name, bases, attrs)
def __call__(cls, *args, **kwargs):