You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Emit signal.worker_init in prefork pool child process #7573
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).
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.
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()
Activity
open-collective-bot commentedon Jun 15, 2022
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.
auvipy commentedon Jun 16, 2022
can you come with proposed patch with an integration & unit test?
milos-u commentedon Jun 29, 2022
I can try, right now a lot of work elsewhere, will take a look at it.
milos-u commentedon May 14, 2025
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:
What is current status of celery/Windows port?
auvipy commentedon May 19, 2025
thanks! can you please open a draft PR? we now have windows in our CI