Skip to content
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

bpo-35715: Liberate return value of _process_worker #11514

Merged
merged 3 commits into from
Mar 16, 2019

Conversation

dchevell
Copy link
Contributor

@dchevell dchevell commented Jan 11, 2019

ProcessPoolExecutor workers will hold the return value of their last task in memory until the next task is received. Since the return value has already been propagated to the parent process's Future or else effectively discarded by this point, the memory can be safely released.

Simple case to reproduce:

import concurrent.futures
import time

executor = concurrent.futures.ProcessPoolExecutor(max_workers=1)

def big_val():
    return [{1:1} for i in range(1, 1000000)]

executor.submit(big_val)

# Observe the memory usage of the process worker during the sleep interval
time.sleep(10)

This should be easily addressed by having the worker explicitly del r after calling _sendback_result as it already does this for call_item.

https://bugs.python.org/issue35715

@the-knights-who-say-ni
Copy link

Hello, and thanks for your contribution!

I'm a bot set up to make sure that the project can legally accept your contribution by verifying you have signed the PSF contributor agreement (CLA).

Our records indicate we have not received your CLA. For legal reasons we need you to sign this before we can look at your contribution. Please follow the steps outlined in the CPython devguide to rectify this issue.

If you have recently signed the CLA, please wait at least one business day
before our records are updated.

You can check yourself to see if the CLA has been received.

Thanks again for your contribution, we look forward to reviewing it!

@dchevell
Copy link
Contributor Author

Signed the PSF contributor agreement

@@ -239,6 +239,7 @@ def _process_worker(call_queue, result_queue, initializer, initargs):
# Liberate the resource as soon as possible, to avoid holding onto
# open files or shared memory that is not needed anymore
del call_item
del r
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks incorrect as this will raise a NameError if the first call to call_item fails because the symbol was never bound.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pablogsal thanks for catching that. I've modified to handle this case and do nothing - although I'm not generally a fan of passing in except clauses it seems appropriate here.

@bedevere-bot
Copy link

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@@ -239,6 +239,10 @@ def _process_worker(call_queue, result_queue, initializer, initargs):
# Liberate the resource as soon as possible, to avoid holding onto
# open files or shared memory that is not needed anymore
del call_item
try:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You just need to delete the result in the else clause of the previous try block unless I am missing something.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair point, thanks @pablogsal !

@dchevell
Copy link
Contributor Author

@pablogsal any other feedback on this one?

@dchevell
Copy link
Contributor Author

Hey @pablogsal, any other feedback or changes needed here?

@pablogsal
Copy link
Member

Sorry for the delay. Please, ping me if I have not landed this this week :)

@dchevell
Copy link
Contributor Author

@pablogsal did you mean to ping you about merging, or did I misunderstand?

No need to apologise for the delay, appreciate you taking the time :)

@pablogsal pablogsal merged commit 962bdea into python:master Mar 16, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants