本文整理汇总了Python中multiprocessing.synchronize.Semaphore.release方法的典型用法代码示例。如果您正苦于以下问题:Python Semaphore.release方法的具体用法?Python Semaphore.release怎么用?Python Semaphore.release使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类multiprocessing.synchronize.Semaphore
的用法示例。
在下文中一共展示了Semaphore.release方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: JoinableQueue
# 需要导入模块: from multiprocessing.synchronize import Semaphore [as 别名]
# 或者: from multiprocessing.synchronize.Semaphore import release [as 别名]
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()
示例2: MyTransport
# 需要导入模块: from multiprocessing.synchronize import Semaphore [as 别名]
# 或者: from multiprocessing.synchronize.Semaphore import release [as 别名]
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()
示例3: MyTransactionManager
# 需要导入模块: from multiprocessing.synchronize import Semaphore [as 别名]
# 或者: from multiprocessing.synchronize.Semaphore import release [as 别名]
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)
示例4: JoinableQueue
# 需要导入模块: from multiprocessing.synchronize import Semaphore [as 别名]
# 或者: from multiprocessing.synchronize.Semaphore import release [as 别名]
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()
示例5: Item
# 需要导入模块: from multiprocessing.synchronize import Semaphore [as 别名]
# 或者: from multiprocessing.synchronize.Semaphore import release [as 别名]
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)
示例6: SshClientFactory
# 需要导入模块: from multiprocessing.synchronize import Semaphore [as 别名]
# 或者: from multiprocessing.synchronize.Semaphore import release [as 别名]
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()
示例7: ApiAsyncWorker
# 需要导入模块: from multiprocessing.synchronize import Semaphore [as 别名]
# 或者: from multiprocessing.synchronize.Semaphore import release [as 别名]
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:")
示例8: __init__
# 需要导入模块: from multiprocessing.synchronize import Semaphore [as 别名]
# 或者: from multiprocessing.synchronize.Semaphore import release [as 别名]
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()