Skip to content

Commit a316c65

Browse files
committed
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.
1 parent 1c80679 commit a316c65

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

pymongo/periodic_executor.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,19 @@ def _on_executor_deleted(ref):
132132
def _shutdown_executors():
133133
# Copy the set. Stopping threads has the side effect of removing executors.
134134
executors = list(_EXECUTORS)
135+
136+
# First signal all executors to close...
135137
for ref in executors:
136-
executor = ref()
137-
if executor:
138-
executor.close()
139-
executor.join(10)
138+
try:
139+
ref().close()
140+
except ReferenceError:
141+
pass
142+
143+
# ...then try to join them.
144+
for ref in executors:
145+
try:
146+
ref().join(1)
147+
except ReferenceError:
148+
pass
140149

141150
atexit.register(_shutdown_executors)

0 commit comments

Comments
 (0)