Pool child process initializer. Initialize the child pool process to ensure the correct app instance is used and things like logging works.
(app, hostname)
| 45 | |
| 46 | |
| 47 | def process_initializer(app, hostname): |
| 48 | """Pool child process initializer. |
| 49 | |
| 50 | Initialize the child pool process to ensure the correct |
| 51 | app instance is used and things like logging works. |
| 52 | """ |
| 53 | # Each running worker gets SIGKILL by OS when main process exits. |
| 54 | platforms.set_pdeathsig('SIGKILL') |
| 55 | _set_task_join_will_block(True) |
| 56 | platforms.signals.reset(*WORKER_SIGRESET) |
| 57 | platforms.signals.ignore(*WORKER_SIGIGNORE) |
| 58 | platforms.set_mp_process_title('celeryd', hostname=hostname) |
| 59 | if get_start_method() != 'fork': |
| 60 | # billiard started this child as a fresh interpreter on its own (for |
| 61 | # example the macOS default since billiard 4.3), so announce it the |
| 62 | # same way the parent does for worker_pool_start_method 'spawn'. |
| 63 | # Both have to be set before init_worker() imports the task modules: |
| 64 | # the environment variable for whatever this process starts later, |
| 65 | # and the flag in the already-imported module that decides how |
| 66 | # @app.task binds. |
| 67 | os.environ['FORKED_BY_MULTIPROCESSING'] = '1' |
| 68 | _app_base.USING_EXECV = True |
| 69 | # This is for Windows and other platforms not supporting |
| 70 | # fork(). Note that init_worker makes sure it's only |
| 71 | # run once per process. |
| 72 | app.loader.init_worker() |
| 73 | app.loader.init_worker_process() |
| 74 | logfile = os.environ.get('CELERY_LOG_FILE') or None |
| 75 | if logfile and '%i' in logfile.lower(): |
| 76 | # logfile path will differ so need to set up logging again. |
| 77 | app.log.already_setup = False |
| 78 | app.log.setup(int(os.environ.get('CELERY_LOG_LEVEL', 0) or 0), |
| 79 | logfile, |
| 80 | bool(os.environ.get('CELERY_LOG_REDIRECT', False)), |
| 81 | str(os.environ.get('CELERY_LOG_REDIRECT_LEVEL')), |
| 82 | hostname=hostname) |
| 83 | if os.environ.get('FORKED_BY_MULTIPROCESSING'): |
| 84 | # the child is a fresh interpreter (spawn, forkserver or execv) |
| 85 | trace.setup_worker_optimizations(app, hostname) |
| 86 | else: |
| 87 | app.set_current() |
| 88 | set_default_app(app) |
| 89 | app.finalize() |
| 90 | trace._tasks = app._tasks # enables fast_trace_task optimization. |
| 91 | # rebuild execution handler for all tasks. |
| 92 | from celery.app.trace import build_tracer |
| 93 | for name, task in app.tasks.items(): |
| 94 | task.__trace__ = build_tracer(name, task, app.loader, hostname, |
| 95 | app=app) |
| 96 | from celery.worker import state as worker_state |
| 97 | worker_state.reset_state() |
| 98 | global _process_hostname |
| 99 | _process_hostname = hostname |
| 100 | signals.worker_process_init.send(sender=hostname) |
| 101 | |
| 102 | |
| 103 | def process_destructor(pid, exitcode): |