本文整理匯總了Python中multiprocessing.Condition.wait_for方法的典型用法代碼示例。如果您正苦於以下問題:Python Condition.wait_for方法的具體用法?Python Condition.wait_for怎麽用?Python Condition.wait_for使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類multiprocessing.Condition
的用法示例。
在下文中一共展示了Condition.wait_for方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: HogwildWorld
# 需要導入模塊: from multiprocessing import Condition [as 別名]
# 或者: from multiprocessing.Condition import wait_for [as 別名]
class HogwildWorld(World):
"""Creates a separate world for each thread (process).
Maintains a few shared objects to keep track of state:
- A Semaphore which represents queued examples to be processed. Every call
of parley increments this counter; every time a Process claims an
example, it decrements this counter.
- A Condition variable which notifies when there are no more queued
examples.
- A boolean Value which represents whether the inner worlds should shutdown.
- An integer Value which contains the number of unprocessed examples queued
(acquiring the semaphore only claims them--this counter is decremented
once the processing is complete).
"""
def __init__(self, world_class, opt, agents):
self.inner_world = world_class(opt, agents)
self.queued_items = Semaphore(0) # counts num exs to be processed
self.epochDone = Condition() # notifies when exs are finished
self.terminate = Value('b', False) # tells threads when to shut down
self.cnt = Value('i', 0) # number of exs that remain to be processed
self.threads = []
for i in range(opt['numthreads']):
self.threads.append(HogwildProcess(i, world_class, opt,
agents, self.queued_items,
self.epochDone, self.terminate,
self.cnt))
for t in self.threads:
t.start()
def __iter__(self):
raise NotImplementedError('Iteration not available in hogwild.')
def display(self):
self.shutdown()
raise NotImplementedError('Hogwild does not support displaying in-run' +
' task data. Use `--numthreads 1`.')
def episode_done(self):
return False
def parley(self):
"""Queue one item to be processed."""
with self.cnt.get_lock():
self.cnt.value += 1
self.queued_items.release()
def getID(self):
return self.inner_world.getID()
def report(self):
return self.inner_world.report()
def save_agents(self):
self.inner_world.save_agents()
def synchronize(self):
"""Sync barrier: will wait until all queued examples are processed."""
with self.epochDone:
self.epochDone.wait_for(lambda: self.cnt.value == 0)
def shutdown(self):
"""Set shutdown flag and wake threads up to close themselves"""
# set shutdown flag
with self.terminate.get_lock():
self.terminate.value = True
# wake up each thread by queueing fake examples
for _ in self.threads:
self.queued_items.release()
# wait for threads to close
for t in self.threads:
t.join()