以下代码对我有用,问题是每个线程必须等到抛出结束或者至少我的感知,因为当我把睡眠(10)指示等待时间然后连续。< / p>
我希望运输线程无需等待内部代码运行。
这是我的代码(示例):
import threading
from time import sleep
class MyThread(threading.Thread):
def __init__(self, num):
threading.Thread.__init__(self)
self.num = num
def run(self):
print "I'm the thread", self.num
sleep(10)
print "I'm the thread, after 10 seg"
print "I'm the main thread"
for i in range(0, 10):
t = MyThread(i)
t.start()
t.join()
感谢您的进步。
答案 0 :(得分:3)
使用2个for
循环:1启动线程,1个等待它们:
# create all threads
ts = [MyThread(i) for i in range(10)]
# start all threads
for t in ts:
t.start()
# wait for all threads
for t in ts:
t.join()