Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to start a Celery worker from a script/module __main__?

Tags:

I've define a Celery app in a module, and now I want to start the worker from the same module in its __main__, i.e. by running the module with python -m instead of celery from the command line. I tried this:

app = Celery('project', include=['project.tasks'])  # do all kind of project-specific configuration # that should occur whenever this module is imported  if __name__ == '__main__':     # log stuff about the configuration     app.start(['worker', '-A', 'project.tasks']) 

but now Celery thinks I'm running the worker without arguments:

Usage: worker <command> [options]   Show help screen and exit.  Options:   -A APP, --app=APP     app instance to use (e.g. module.attr_name) [snip] 

The usage message is the one you get from celery --help, as if it didn't get a command. I've also tried

app.worker_main(['-A', 'project.tasks']) 

but that complains about the -A not being recognized.

So how do I do this? Or alternatively, how do I pass a callback to the worker to have it log information about its configuration?

like image 220
Fred Foo Avatar asked Apr 30 '14 13:04

Fred Foo


People also ask

How does Celery workers Python?

Celery is an open-source Python library which is used to run the tasks asynchronously. It is a task queue that holds the tasks and distributes them to the workers in a proper manner. It is primarily focused on real-time operation but also supports scheduling (run regular interval tasks).


2 Answers

using app.worker_main method (v3.1.12):

± cat start_celery.py #!/usr/bin/python  from myapp import app   if __name__ == "__main__":     argv = [         'worker',         '--loglevel=DEBUG',     ]     app.worker_main(argv) 
like image 127
okocian Avatar answered Sep 28 '22 03:09

okocian


Based on code from Django-Celery module you could try something like this:

from __future__ import absolute_import, unicode_literals  from celery import current_app from celery.bin import worker   if __name__ == '__main__':     app = current_app._get_current_object()      worker = worker.worker(app=app)      options = {         'broker': 'amqp://guest:guest@localhost:5672//',         'loglevel': 'INFO',         'traceback': True,     }      worker.run(**options) 
like image 36
daniula Avatar answered Sep 28 '22 04:09

daniula