线程没有在Python中再次创建

时间:2014-11-13 20:21:34

标签: python multithreading

我有一些看起来像这样的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循环的每次迭代重新启动线程。谁能告诉我什么是错的以及如何解决它?

1 个答案:

答案 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