Skip to content

Instantly share code, notes, and snippets.

@aeros
Last active January 22, 2020 05:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aeros/d1ff62b730426584413bca0c8f2ed99d to your computer and use it in GitHub Desktop.
Save aeros/d1ff62b730426584413bca0c8f2ed99d to your computer and use it in GitHub Desktop.
"""Warning: executor.shutdown(wait=False) causes deadlocks for instances of
concurrent.futures.ProcessPoolExecutor.
Not intended for usage outside of debugging and demonstration purposes.
Tested on Python 3.7.6 and 3.8.1.
"""
import concurrent.futures as cf
import time
def wait_return(duration, result):
time.sleep(duration)
return result
def show_executor_shutdown(executor_type, wait):
executor = executor_type(max_workers=3)
futures = [executor.submit(wait_return, .1, 42) for _ in range(9)]
executor.shutdown(wait=wait)
for fut in futures:
print(f"fut._result = {fut._result}, fut._state = {fut._state}")
if __name__ == "__main__":
# Behaves as expected
print("ThreadPoolExecutor:")
print("wait=False")
show_executor_shutdown(executor_type=cf.ThreadPoolExecutor,
wait=False)
print()
print("wait=True")
show_executor_shutdown(executor_type=cf.ThreadPoolExecutor,
wait=True)
print("ProcessPoolExecutor:")
print("wait=True")
show_executor_shutdown(executor_type=cf.ProcessPoolExecutor,
wait=True)
print()
print("wait=False")
# Deadlock
show_executor_shutdown(executor_type=cf.ProcessPoolExecutor,
wait=False)
print()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment