-
-
Notifications
You must be signed in to change notification settings - Fork 31.8k
concurrent.futures ThreadPoolExecutor keeps unnecessary references to worker functions. #60488
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
The ThreadPoolExecutor unnecessarily keeps references to _WorkItem objects. With the attached patch (which lacks a test), all tests still pass, and the references are removed as soon as they're no longer needed. |
A new patch (with tests), and a fuller explanation: At work, we've got Python talking to a customer's existing COM library; we're using Thomas Heller's 'comtypes' library to do that. Unfortunately, comtypes depends quite a lot on __del__-time cleanup, so reference counting matters. (I'm well aware that this isn't the recommended way to deal with resource cleanup in Python, but rewriting the existing infrastructure isn't a realistic option here.) Anyway, it turned out that the concurrent.futures executors were indirectly holding onto references to COM objects, causing issues with our application. The attached patch adds a few 'del' statements to remove references that are no longer needed. For the ProcessExecutor, some of those 'del' statements had to go into the multiprocessing.Queue implementation. The troublesome pattern (in both multiprocessing and futures) takes the form (simplified): def my_worker_function(...):
...
while <exit_condition_not_satisfied>:
obj = blocking_wait_for_next_item()
do_processing(obj)
... The issue is that the reference to obj is kept until the completion of the next blocking wait call. I'm suggesting just adding an extra 'del obj' after 'do_processing(obj)'. |
Sounds fine to me. You might want to make the test CPython-specific. |
The concurrent.futures stuff looks good to me. Could you add a comment explaining why the delete is necessary? And, as Antoine said, the test should be CPython only. |
LGTM |
Updated patch to execute tests only for CPython. |
Added comments to patch |
New changeset 70cef0a160cf by Andrew Svetlov in branch 'default': |
Committed. Thanks. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: