如何设置它以便线程通信他们完成任务?

时间:2009-01-21 18:59:26

标签: python multithreading

从概念上讲,我想完成以下操作,但在理解如何在Python中正确编码时遇到了麻烦:

from threading import Thread
for i in range(0,3):
    t = Thread(target=myfunction)
    t.start()

# wait until threads have finished executing
print 'complete!'

2 个答案:

答案 0 :(得分:6)

将线程添加到列表中并join()

from threading import Thread
tlist = []
for i in range(3):
    t = Thread(target=some_function)
    t.start()
    tlist.append(t)

# wait until threads have finished executing
for t in tlist:
    t.join()

print 'complete!'

答案 1 :(得分:-3)

我从未使用过python,但我认为你所寻找的概念是“信号量”。

谷歌出现了这个:

http://www.python.org/doc/2.5.2/lib/semaphore-objects.html