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

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

    I use a slightly different technique because of the nature of the threads I used in my application. To illustrate, this is a fragment of a test-strap program I wrote to scaffold a barrier class for my threading class:

       while threads:
            finished = set(threads) - set(threading.enumerate())
            while finished:
                ttt = finished.pop()
                threads.remove(ttt)
            time.sleep(0.5)
    

    Why do I do it this way? In my production code, I have a time limit, so the first line actually reads "while threads and time.time() < cutoff_time". If I reach the cut-off, I then have code to tell the threads to shut down.

提交回复
热议问题