[英]How to start a Celery worker from a script/module __main__?
I've define a Celery
app in a module, and now I want to start the worker from the same module in its __main__
, ie by running the module with python -m
instead of celery
from the command line.我已经在一个模块中定义了一个Celery
应用程序,现在我想从__main__
的同一个模块启动工作程序,即通过从命令行使用python -m
而不是celery
运行模块。 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:但现在 Celery 认为我在不加参数地运行工人:
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.使用消息是您从celery --help
获得的消息,就好像它没有收到命令一样。 I've also tried我也试过
app.worker_main(['-A', 'project.tasks'])
but that complains about the -A
not being recognized.但这抱怨-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?或者,我如何将回调传递给工作人员以使其记录有关其配置的信息?
using app.worker_main method (v3.1.12):使用 app.worker_main 方法(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)
Based on code from Django-Celery module you could try something like this:基于来自 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)
The worker_main
results now: worker_main
现在的结果:
AttributeError: 'Celery' object has no attribute 'worker_main'
app = celery.Celery(
'project',
include=['project.tasks']
)
if __name__ == '__main__':
worker = app.Worker(
include=['project.tasks']
)
worker.start()
See here celery.apps.worker and celery.worker.WorkController.setup_defaults for details (hope it will be documented better in the future).有关详细信息,请参阅此处celery.apps.worker和celery.worker.WorkController.setup_defaults (希望将来能更好地记录)。
worker_main
was put back in celery 5.0.3 here: https://github.com/celery/celery/pull/6481 worker_main
被放回 celery 5.0.3 中: https : //github.com/celery/celery/pull/6481
This worked for me on 5.0.4:这在 5.0.4 上对我有用:
self.app.worker_main(argv = ['worker', '--loglevel=info', '--concurrency={}'.format(os.environ['CELERY_CONCURRENCY']), '--without-gossip'])
I think you are just missing wrapping the args so celery can read them, like:我认为您只是缺少包装 args 以便 celery 可以读取它们,例如:
queue = Celery('blah', include=['blah'])
queue.start(argv=['celery', 'worker', '-l', 'info'])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.