PYTHON-939 - Stop background threads quicker.

Instead of signaling each thread to close, then waiting for it to finish
before signaling the next, instead signal all threads to close and let
them stop in parallel before joining any.

Additionally, spend less time holding strong references to threads.
This commit is contained in:
A. Jesse Jiryu Davis 2015-06-15 13:29:03 -04:00
parent 1c806791a9
commit a316c6576d

View File

@ -132,10 +132,19 @@ def _on_executor_deleted(ref):
def _shutdown_executors():
# Copy the set. Stopping threads has the side effect of removing executors.
executors = list(_EXECUTORS)
# First signal all executors to close...
for ref in executors:
executor = ref()
if executor:
executor.close()
executor.join(10)
try:
ref().close()
except ReferenceError:
pass
# ...then try to join them.
for ref in executors:
try:
ref().join(1)
except ReferenceError:
pass
atexit.register(_shutdown_executors)