In Python threading, how I can I track a thread's completion?

前端 未结 7 1885
暖寄归人
暖寄归人 2020-12-19 04:52

I\'ve a python program that spawns a number of threads. These threads last anywhere between 2 seconds to 30 seconds. In the main thread I want to track whenever each thread

7条回答
  •  独厮守ぢ
    2020-12-19 05:36

    What I would suggest is loop like this

    while len(threadSet) > 0:
        time.sleep(1)
        for thread in theadSet:
            if not thread.isAlive()
                print "Thread "+thread.getName()+" terminated"
                threadSet.remove(thread)
    

    There is a 1 second sleep, so there will be a slight delay between the thread termination and the message being printed. If you can live with this delay, then I think this is a simpler solution than the one you proposed in your question.

提交回复
热议问题