|
66 | 66 | # workers to exit when their work queues are empty and then waits until the |
67 | 67 | # threads/processes finish. |
68 | 68 |
|
69 | | -_thread_references = set() |
| 69 | +_live_threads = weakref.WeakSet() |
70 | 70 | _shutdown = False |
71 | 71 |
|
72 | 72 | def _python_exit(): |
73 | 73 | global _shutdown |
74 | 74 | _shutdown = True |
75 | | - for thread_reference in _thread_references: |
76 | | - thread = thread_reference() |
77 | | - if thread is not None: |
78 | | - thread.join() |
79 | | - |
80 | | -def _remove_dead_thread_references(): |
81 | | - """Remove inactive threads from _thread_references. |
82 | | -
|
83 | | - Should be called periodically to prevent memory leaks in scenarios such as: |
84 | | - >>> while True: |
85 | | - >>> ... t = ThreadPoolExecutor(max_workers=5) |
86 | | - >>> ... t.map(int, ['1', '2', '3', '4', '5']) |
87 | | - """ |
88 | | - for thread_reference in set(_thread_references): |
89 | | - if thread_reference() is None: |
90 | | - _thread_references.discard(thread_reference) |
| 75 | + for thread in _live_threads: |
| 76 | + thread.join() |
91 | 77 |
|
92 | 78 | # Controls how many more calls than processes will be queued in the call queue. |
93 | 79 | # A smaller number will mean that processes spend more time idle waiting for |
@@ -279,7 +265,6 @@ def __init__(self, max_workers=None): |
279 | 265 | worker processes will be created as the machine has processors. |
280 | 266 | """ |
281 | 267 | _check_system_limits() |
282 | | - _remove_dead_thread_references() |
283 | 268 |
|
284 | 269 | if max_workers is None: |
285 | 270 | self._max_workers = multiprocessing.cpu_count() |
@@ -316,7 +301,7 @@ def _start_queue_management_thread(self): |
316 | 301 | self._shutdown_process_event)) |
317 | 302 | self._queue_management_thread.daemon = True |
318 | 303 | self._queue_management_thread.start() |
319 | | - _thread_references.add(weakref.ref(self._queue_management_thread)) |
| 304 | + _live_threads.add(self._queue_management_thread) |
320 | 305 |
|
321 | 306 | def _adjust_process_count(self): |
322 | 307 | for _ in range(len(self._processes), self._max_workers): |
|
0 commit comments