3 from __future__ import with_statement
15 _moduleLogger = logging.getLogger(__name__)
18 class AsyncLinearExecution(object):
20 def __init__(self, pool, func):
25 def start(self, *args, **kwds):
26 assert self._run is None
27 self._run = self._func(*args, **kwds)
28 trampoline, args, kwds = self._run.send(None) # priming the function
37 @misc.log_exception(_moduleLogger)
38 def on_success(self, result):
39 _moduleLogger.debug("Processing success for: %r", self._func)
41 trampoline, args, kwds = self._run.send(result)
42 except StopIteration, e:
53 @misc.log_exception(_moduleLogger)
54 def on_error(self, error):
55 _moduleLogger.debug("Processing error for: %r", self._func)
57 trampoline, args, kwds = self._run.throw(error)
58 except StopIteration, e:
70 return "<async %s at 0x%x>" % (self._func.__name__, id(self))
73 return hash(self._func)
75 def __eq__(self, other):
76 return self._func == other._func
78 def __ne__(self, other):
79 return self._func != other._func
82 def synchronized(lock):
84 Synchronization decorator.
87 >>> misc.validate_decorator(synchronized(object()))
93 def newFunction(*args, **kw):
103 @contextlib.contextmanager
104 def qlock(queue, gblock = True, gtimeout = None, pblock = True, ptimeout = None):
106 Locking with a queue, good for when you want to lock an item passed around
110 >>> lock = Queue.Queue()
112 >>> with qlock(lock) as i:
116 item = queue.get(gblock, gtimeout)
120 queue.put(item, pblock, ptimeout)
123 @contextlib.contextmanager
124 def flock(path, timeout=-1):
131 while timeSpent <= timeout or timeout == WAIT_FOREVER:
133 fd = os.open(path, os.O_CREAT | os.O_EXCL | os.O_RDWR)
137 if e.errno != errno.EEXIST:
142 assert acquired, "Failed to grab file-lock %s within timeout %d" % (path, timeout)