How do you kill Futures once they have started?

前端 未结 2 1799
北海茫月
北海茫月 2020-12-13 08:34

I am using the new concurrent.futures module (which also has a Python 2 backport) to do some simple multithreaded I/O. I am having trouble understanding how to cleanly kill

2条回答
  •  情深已故
    2020-12-13 09:15

    It's kind of painful. Essentially, your worker threads have to be finished before your main thread can exit. You cannot exit unless they do. The typical workaround is to have some global state, that each thread can check to determine if they should do more work or not.

    Here's the quote explaining why. In essence, if threads exited when the interpreter does, bad things could happen.

    Here's a working example. Note that C-c takes at most 1 sec to propagate because the sleep duration of the child thread.

    #!/usr/bin/env python
    from __future__ import print_function
    
    import concurrent.futures
    import time
    import sys
    
    quit = False
    def wait_a_bit(name):
        while not quit:
            print("{n} is doing work...".format(n=name))
            time.sleep(1)
    
    def setup():
        executor = concurrent.futures.ThreadPoolExecutor(max_workers=5)
        future1 = executor.submit(wait_a_bit, "Jack")
        future2 = executor.submit(wait_a_bit, "Jill")
    
        # main thread must be doing "work" to be able to catch a Ctrl+C 
        # http://www.luke.maurits.id.au/blog/post/threads-and-signals-in-python.html
        while (not (future1.done() and future2.done())):
            time.sleep(1)
    
    if __name__ == "__main__":
        try:
            setup()
        except KeyboardInterrupt:
            quit = True
    

提交回复
热议问题