class JoinableQueue(Queue): def __init__(self, maxsize=0): Queue.__init__(self, maxsize) self._unfinished_tasks = Semaphore(0) self._cond = Condition() def __getstate__(self): return Queue.__getstate__(self) + (self._cond, self._unfinished_tasks) def __setstate__(self, state): Queue.__setstate__(self, state[:-2]) self._cond, self._unfinished_tasks = state[-2:] def put(self, item, block=True, timeout=None): Queue.put(self, item, block, timeout) self._unfinished_tasks.release() def task_done(self): self._cond.acquire() try: if not self._unfinished_tasks.acquire(False): raise ValueError('task_done() called too many times') if self._unfinished_tasks._semlock._is_zero(): self._cond.notify_all() finally: self._cond.release() def join(self): self._cond.acquire() try: if not self._unfinished_tasks._semlock._is_zero(): self._cond.wait() finally: self._cond.release()
class MyTransport(iIpcTransport): def __init__(self, *args, **kwargs): self.dataSent = [] self.dataSentLock = Semaphore(0) def sendData(self, result, transactionId): self.dataSent.append((transactionId, result)) self.dataSentLock.release()
def _build_single_scenario_proc(clean: bool, allow_offset_map: bool, scenario: str, semaphore: synchronize.Semaphore): semaphore.acquire() try: _build_single_scenario(clean, allow_offset_map, scenario) finally: semaphore.release()
class MyTransactionManager(TransactionManager): def __init__(self): super(MyTransactionManager, self).__init__(StandardTransactionGenerator()) self.lastTid = None self.sem = Semaphore(0) self.result = [] def next(self): self.lastTid = super(MyTransactionManager, self).next() return self.lastTid def release(self, tId, result): self.sem.release() self.result.append((tId, result)) super(MyTransactionManager, self).release(tId, result)
class JoinableQueue(Queue): def __init__(self, maxsize = 0): Queue.__init__(self, maxsize) self._unfinished_tasks = Semaphore(0) self._cond = Condition() def __getstate__(self): return Queue.__getstate__(self) + (self._cond, self._unfinished_tasks) def __setstate__(self, state): Queue.__setstate__(self, state[:-2]) self._cond, self._unfinished_tasks = state[-2:] def put(self, obj, block = True, timeout = None): if not not self._closed: raise AssertionError raise self._sem.acquire(block, timeout) or Full self._notempty.acquire() self._cond.acquire() try: if self._thread is None: self._start_thread() self._buffer.append(obj) self._unfinished_tasks.release() self._notempty.notify() finally: self._cond.release() self._notempty.release() return def task_done(self): self._cond.acquire() try: if not self._unfinished_tasks.acquire(False): raise ValueError('task_done() called too many times') if self._unfinished_tasks._semlock._is_zero(): self._cond.notify_all() finally: self._cond.release() def join(self): self._cond.acquire() try: if not self._unfinished_tasks._semlock._is_zero(): self._cond.wait() finally: self._cond.release()
class Item(object): def __init__(self, i): self._i = i self._sem = Semaphore(0) self._result = [] def acquire(self, timeout=None): return self._sem.acquire(block=True, timeout=timeout) def result(self, result): self._result.append(result) # Now the result is complete, release it: self._sem.release() def getResult(self): return self._result.pop(0)
class SshClientFactory(object): DEFAULT_PORT = 22 def __init__(self, host, port, username, password, dataQ=None, exitCmd="exit", sshType=SshType.STB, postConnectionTimeout=0): self.username = username self.password = password self.exitCmd = exitCmd self.postConnectionTimeout = postConnectionTimeout self.host = host self.sshType = sshType if port==None: port = SshClientFactory.DEFAULT_PORT self.port = int(port) if dataQ==None: def dummyPut(*args, **kwargs): sys.stderr.write("SshClientFactory consumed data!\n") dataQ = dummyPut self.dataQ = dataQ # self.isClosing = False self.lastSendTime = time.time() # self.connection = None self.run() def run(self): sys.stderr.write("SshClientFactory running!\n") self.factory = SSHFactory(self) self.sem = Semaphore(0) def _connectLater(): sys.stderr.write("SshClientFactory connecting asynchronously\n") reactor.connectTCP(self.host, self.port, self.factory) #@UndefinedVariable sys.stderr.write("SshClientFactory connected\n") threads.blockingCallFromThread(reactor, _connectLater) #@UndefinedVariable self.sem.acquire() def send(self, data, tId, timeout=None): client = ClientData(self, cmd=data, tId=tId) reactor.callFromThread(self.connection.openAChannel, client) #@UndefinedVariable def terminate(self): self.isClosing = True if reactor.running: #@UndefinedVariable reactor.stop() #@UndefinedVariable def isOk(self): return reactor.running #@UndefinedVariable def serviceStarted(self): self.sem.release()
class ApiAsyncWorker(threading.Thread): ID = itertools.count(1) @staticmethod def startAll(workers=[]): for worker in workers: worker.start() for worker in workers: worker.waitUntilRunning() @staticmethod def create(q, parent, start=False): worker = ApiAsyncWorker(q, parent) if start: ApiAsyncWorker.startAll([worker]) return worker def __init__(self, q, parent): super(ApiAsyncWorker, self).__init__() self._q = q self._parent = parent self._name = "ApiAsyncWorker_%(C)s" % {"C":ApiAsyncWorker.ID.next()} self._logger = LogManager().getLogger(self._name) self.setDaemon(True) self.setName(self._name) self._cancel = False self._runLock = Semaphore(0) def stop(self): self._cancel = True def waitUntilRunning(self, timeout=None): # Wait until we become running: return self._runLock.acquire(block=True, timeout=timeout) def run(self): self._logger.debug("Thread running...") self._runLock.release() # Now do the work: try: self._work() except EOFError, _e: self._logger.warn("EOF in thread...") except Exception, _e: self._logger.exception("Unhandled error in thread:")
class Barrier: """ Excerpt from the Semaphore book by Downey 08 """ def __init__(self, n, count): self.n = n self.count = count self.count[...] = 0 self.mutex = Semaphore(1) self.turnstile = Semaphore(0) self.turnstile2 = Semaphore(0) def abort(self): """ ensure the master exit from Barrier """ self.mutex.release() self.turnstile.release() self.mutex.release() self.turnstile2.release() def phase1(self): try: self.mutex.acquire() self.count[...] += 1 if self.count == self.n: [self.turnstile.release() for i in range(self.n)] finally: self.mutex.release() self.turnstile.acquire() def phase2(self): try: self.mutex.acquire() self.count[...] -= 1 if self.count == 0: [self.turnstile2.release() for i in range(self.n)] finally: self.mutex.release() self.turnstile2.acquire() def wait(self): if self.n == 0: return self.phase1() self.phase2()