如何从脚本/模块__main__启动Celery worker? [英] How to start a Celery worker from a script/module __main__?
问题描述
我已经在一个模块中定义了一个 Celery
应用程序,现在我想从其 __ main__中的同一模块启动worker。 code>,即通过在命令行中使用
python -m
而不是 celery
运行模块。我尝试过这个:
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'])
但现在Celery认为我正在运行没有参数的工作器:
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]
用法消息是您从 celery --help
收到的消息,好像没有没有命令。我也尝试过
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'])
但这抱怨 -A
无法被识别。
那么我该怎么做?或者,如何将回调传递给工作程序以使其记录有关其配置的信息?
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?
推荐答案
基于来自Django-Celery模块的代码像这样的东西:
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)
这篇关于如何从脚本/模块__main__启动Celery worker?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!