Skip to content

Emit signal.worker_init in prefork pool child process #7573

Open

Description

@milos-u

Checklist

  • I have checked the issues list
    for similar or identical enhancement to an existing feature.
  • I have checked the pull requests list
    for existing proposed enhancements.
  • I have checked the commit log
    to find out if the if the same enhancement was already implemented in the
    master branch.
  • I have included all related issues and possible duplicate issues in this issue
    (If there are none, check this box anyway).

Related Issues and Possible Duplicates

Related Issues

  • None

Possible Duplicates

  • None

Brief Summary

From my manual observation, child process from the prefork pool does not emit signal.worker_init under Windows when starting.

With this patch:

--- a/celery/concurrency/prefork.py
+++ b/celery/concurrency/prefork.py
@@ -78,6 +78,7 @@ def process_initializer(app, hostname):
                                       app=app)
     from celery.worker import state as worker_state
     worker_state.reset_state()
+    signals.worker_init.send(sender=None)
     signals.worker_process_init.send(sender=None)

first the Django worker_fixup is installed (in worker_init signal callback) and then Django fixup on_worker_process_init is called (in worker_process_init signal callback).

Without this patch, Django is not initialized inside the child process (django.setup() is not called).

Design

Architectural Considerations

None

Proposed Behavior

Proposed UI/UX

Diagrams

N/A

Alternatives

None

Activity

  1. open-collective-bot commented on Jun 15, 2022

    @open-collective-bot

    Hey @milos-u 👋,
    Thank you for opening an issue. We will get back to you as soon as we can.
    Also, check out our Open Collective and consider backing us - every little helps!

    We also offer priority support for our sponsors.
    If you require immediate assistance please consider sponsoring us.

  2. auvipy commented on Jun 16, 2022

    @auvipy
    Member

    can you come with proposed patch with an integration & unit test?

  3. milos-u commented on Jun 29, 2022

    @milos-u
    Author

    I can try, right now a lot of work elsewhere, will take a look at it.

  4. milos-u commented on May 14, 2025

    @milos-u
    Author

    I finally had a time after 3 years to get into the "Celery prefork does not work under Windows" issue and the solutions looks to be quite simple - to get celery with prefork pool working under windows.

    Simple diff against 5.3beta in my private repo is like the following:

     celery/app/trace.py           | 11 +++++++++++
     celery/concurrency/prefork.py | 17 ++++++++++++++---
     2 files changed, 25 insertions(+), 3 deletions(-)
    
    diff --git a/celery/app/trace.py b/celery/app/trace.py
    index 5ae79a07e..81b0e92a2 100644
    --- a/celery/app/trace.py
    +++ b/celery/app/trace.py
    @@ -684,6 +684,12 @@ def report_internal_error(task, exc):
     
     
     def setup_worker_optimizations(app, hostname=None):
    +    """Setup worker related optimizations."""
    +    setup_worker_optimizations_stage1(app, hostname=hostname)
    +    setup_worker_optimizations_stage2(app, hostname=hostname)
    +
    +
    +def setup_worker_optimizations_stage1(app, hostname=None):
         """Setup worker related optimizations."""
         hostname = hostname or gethostname()
     
    @@ -700,6 +706,11 @@ def setup_worker_optimizations(app, hostname=None):
         app.set_current()
         app.set_default()
     
    +
    +def setup_worker_optimizations_stage2(app, hostname):
    +    """Setup worker fast track optimizations."""
    +    hostname = hostname or gethostname()
    +
         # evaluate all task classes by finalizing the app.
         app.finalize()
     
    diff --git a/celery/concurrency/prefork.py b/celery/concurrency/prefork.py
    index 4f32b83b9..d98c23999 100644
    --- a/celery/concurrency/prefork.py
    +++ b/celery/concurrency/prefork.py
    @@ -50,8 +50,6 @@ def process_initializer(app, hostname):
         # This is for Windows and other platforms not supporting
         # fork().  Note that init_worker makes sure it's only
         # run once per process.
    -    app.loader.init_worker()
    -    app.loader.init_worker_process()
         logfile = os.environ.get('CELERY_LOG_FILE') or None
         if logfile and '%i' in logfile.lower():
             # logfile path will differ so need to set up logging again.
    @@ -62,9 +60,22 @@ def process_initializer(app, hostname):
                       str(os.environ.get('CELERY_LOG_REDIRECT_LEVEL')),
                       hostname=hostname)
         if os.environ.get('FORKED_BY_MULTIPROCESSING'):
    +        trace.setup_worker_optimizations_stage1(app, hostname)
    +        # This is for Windows and other platforms not supporting
    +        # fork().  Note that init_worker makes sure it's only
    +        # run once per process.
             # pool did execv after fork
    -        trace.setup_worker_optimizations(app, hostname)
    +        app.loader.init_worker()
    +        # pool did execv after fork
    +        app.loader.init_worker_process()
    +        trace.setup_worker_optimizations_stage2(app, hostname)
         else:
    +        # This is for Windows and other platforms not supporting
    +        # fork().  Note that init_worker makes sure it's only
    +        # run once per process.
    +        # pool did execv after fork
    +        app.loader.init_worker()
    +        app.loader.init_worker_process()
             app.set_current()
             set_default_app(app)
             app.finalize()
    
    

    What is current status of celery/Windows port?

  5. auvipy commented on May 19, 2025

    @auvipy
    Member

    thanks! can you please open a draft PR? we now have windows in our CI

  6. added this to the 5.7.0 milestone on May 19, 2025
  7. modified the milestones: 5.7.0, 6.0, 5.8.0 on Aug 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions