Repository navigation
docs: explain gc.freeze() for prefork memory sharing - #11173
Open
marianoInsa wants to merge 2 commits into
Open
marianoInsa wants to merge 2 commits into
marianoInsa wants to merge 2 commits into
Conversation
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The optimizing guide covers the prefork pool's copy-on-write sharing and the
fork/spawntrade-offs, but not how the cyclic garbage collector breaks thatsharing nor how
gc.freeze()can stop it.This adds a "Memory sharing with the prefork pool" section that documents two
options:
gc.collect()+gc.freeze()fromworker_init: a single hook, but thecollection can leave free pages that children reuse, so sharing is only
partly preserved.
gcdocumentation:gc.disable()before theapp and task imports,
gc.freeze()fromworker_initandworker_before_create_process, andgc.enable()fromworker_process_init.The section also covers the implementation limits:
worker_before_create_processis sent byAsynPool;BlockingPooldoes notsend it, so
worker_initis the fallback there.AsynPoolcallsgc.collect()immediately after the signal, andgc.disable()does not skip explicit collections.gc.disable()affects every process that imports the module, so the exampleuses a worker-only module.
gc.freeze()only freezes the objects that exist at that moment.forkstart method and leaves themaster's collector disabled.
Fixes #11074References
gc.freeze():https://docs.python.org/3.11/library/gc.html#gc.freeze
gc.freeze():https://docs.python.org/3.12/library/gc.html#gc.freeze
Modules/gcmodule.c:https://github.com/python/cpython/blob/main/Modules/gcmodule.c
Verification
sphinx-build -j auto -b html docs docs/_build/html: build succeeded, nowarnings from
docs/userguide/optimizing.rst.pre-commit run --files docs/userguide/optimizing.rst(excluding mypy):passed.