本文整理汇总了Python中threading.Thread.event方法的典型用法代码示例。如果您正苦于以下问题:Python Thread.event方法的具体用法?Python Thread.event怎么用?Python Thread.event使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类threading.Thread
的用法示例。
在下文中一共展示了Thread.event方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_multiple_threads
# 需要导入模块: from threading import Thread [as 别名]
# 或者: from threading.Thread import event [as 别名]
def test_multiple_threads(iterations=120, num_consumers=30):
q = BlockingQueue(iterations)
woken_thread_event = Event()
threads = []
times = []
def _get(iters, ev):
start = time()
for _i in xrange(iters):
q.get()
ev.set()
woken_thread_event.set()
end = time()
times.append(end-start)
iters_per_consumer = iterations/num_consumers
for _i in range(num_consumers):
ev = Event()
tc = Thread(target=_get, args=(iters_per_consumer, ev))
tc.setDaemon(1)
tc.event = ev
threads.append(tc)
for t in threads:
t.start()
assert not times
for i in xrange(iterations):
q.put(i)
woken_thread_event.wait(1)
woken_thread_event.clear()
woken_threads = [t for t in threads if t.event.isSet()]
assert len(woken_threads) == 1, woken_threads
woken_threads[0].event.clear()
for t in threads:
t.join(1)
assert not t.isAlive()
assert len(times) == len(threads)