主题!为什么我在Thread中使用join(timeout)但Thread不会停止

时间:2017-11-15 04:19:08

标签: python python-2.7

我尝试使用“join”来管理和停止花费太多时间的线程。而且,我在线程中使用“While”来保持线程始终运行。但是,我发现这些线程永远不会停止。如何管理花费太多时间的线程? 谢谢!

import threading, time


def doThreadTest():
    print 'start thread time:', time.strftime('%H:%M:%S')
    while 1:
        print 1
    print 'stop thread time:', time.strftime('%H:%M:%S')


threads = []
for i in range(2):
    thread1 = threading.Thread(target=doThreadTest)
    # thread1.setDaemon(True)

    threads.append(thread1)

for t in threads:
    t.start()

for t in threads:
    t.join(5)  # I use join(5) to stop a thread if it cost more than 5s
print 'stop main thread', time.strftime('%H:%M:%S')

0 个答案:

没有答案