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

前端 未结 7 1866
暖寄归人
暖寄归人 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:53

    I'm not sure I see the problem with using: threading.activeCount()

    to track the number of threads that are still active?

    Even if you don't know how many threads you're going to launch before starting it seems pretty easy to track. I usually generate thread collections via list comprehension then a simple comparison using activeCount to the list size can tell you how many have finished.

    See here: http://docs.python.org/library/threading.html

    Alternately, once you have your thread objects you can just use the .isAlive method within the thread objects to check.

    I just checked by throwing this into a multithread program I have and it looks fine:

    for thread in threadlist:
            print(thread.isAlive())
    

    Gives me a list of True/False as the threads turn on and off. So you should be able to do that and check for anything False in order to see if any thread is finished.

    0 讨论(0)
提交回复
热议问题