我有一些看起来像这样的Python代码:
from ltpd import *
def thread_function():
for i in range(5):
if activatewindow('Confirm New Extension'):
generatekeyevent('<left><space>')
break
time.sleep(1)
def main():
for i in range some_big_range:
thread = Thread(target = thread_function)
thread.start()
# Code that runs for really long time
我期待为循环中的每个i
创建一个新线程。但是,该线程只创建一次。我需要为for循环的每次迭代重新启动线程。谁能告诉我什么是错的以及如何解决它?
答案 0 :(得分:1)
每次迭代新线程都是startet:
>>> from threading import Thread
>>> def fun(cnt):
... print cnt
...
>>> for i in range(5):
... thread = Thread(target=fun, args=(i,))
... thread.start()
...
0
1
2
3
>>> 4