Skip to content

docs: explain gc.freeze() for prefork memory sharing - #11173

Open
marianoInsa wants to merge 2 commits into
celery:mainfrom
marianoInsa:docs/prefork-gc-freeze
Open

marianoInsa wants to merge 2 commits into
celery:mainfrom
marianoInsa:docs/prefork-gc-freeze

Conversation

@marianoInsa

Copy link
Copy Markdown

Summary

The optimizing guide covers the prefork pool's copy-on-write sharing and the
fork/spawn trade-offs, but not how the cyclic garbage collector breaks that
sharing 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() from worker_init: a single hook, but the
    collection can leave free pages that children reuse, so sharing is only
    partly preserved.
  • The pattern from the Python gc documentation: gc.disable() before the
    app and task imports, gc.freeze() from worker_init and
    worker_before_create_process, and gc.enable() from
    worker_process_init.

The section also covers the implementation limits:

  • worker_before_create_process is sent by AsynPool; BlockingPool does not
    send it, so worker_init is the fallback there.
  • AsynPool calls gc.collect() immediately after the signal, and
    gc.disable() does not skip explicit collections.
  • gc.disable() affects every process that imports the module, so the example
    uses a worker-only module.
  • Each gc.freeze() only freezes the objects that exist at that moment.
  • The pattern requires prefork with the fork start method and leaves the
    master's collector disabled.
  • Measure with PSS instead of RSS.

Fixes #11074

References

Verification

  • sphinx-build -j auto -b html docs docs/_build/html: build succeeded, no
    warnings from docs/userguide/optimizing.rst.
  • pre-commit run --files docs/userguide/optimizing.rst (excluding mypy):
    passed.

This branch has not been deployed

No deployments
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.

2 participants