startover / pythonfutures

Automatically exported from code.google.com/p/pythonfutures
Other
0 stars 0 forks source link

Improvement: Faster exit on shutdown for thread pooling #21

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
I propose changing
        while True:
            try:
                work_item = work_queue.get(block=True, timeout=0.1)
            except queue.Empty:
                executor = executor_reference()
                # Exit if:
                #   - The interpreter is shutting down OR
                #   - The executor that owns the worker has been collected OR
                #   - The executor that owns the worker has been shutdown.
                if _shutdown or executor is None or executor._shutdown:
                    return
                del executor
            else:
                work_item.run()

To
        while True:
            executor = executor_reference()
            # Exit if:
            #   - The interpreter is shutting down OR
            #   - The executor that owns the worker has been collected OR
            #   - The executor that owns the worker has been shutdown.
            if _shutdown or executor is None or executor._shutdown:
                return
            del executor
            try:
                work_item = work_queue.get(block=True, timeout=0.1)
            except queue.Empty:
                pass
            else:
                work_item.run()

This would allow you not to have to wait for workers to go through the pool. 
Since we just wait in shutdown for the threads to finish, it should (as far as 
I can see) be safe to just make the threads unconditionally check whether they 
should exit and do so. As it is, you *have to* wait for all submitted work to 
finish before exiting interpreter which is imo inconvenient.

Original issue reported on code.google.com by seppo.yl...@gmail.com on 10 Jun 2013 at 9:24