Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/magento/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何从脚本/模块\uuuuu main\uuuuuuuu启动芹菜工作程序?_Python_Celery_Celeryd - Fatal编程技术网

Python 如何从脚本/模块\uuuuu main\uuuuuuuu启动芹菜工作程序?

Python 如何从脚本/模块\uuuuu main\uuuuuuuu启动芹菜工作程序?,python,celery,celeryd,Python,Celery,Celeryd,我已经在一个模块中定义了一个芹菜应用程序,现在我想从它的\uuuuuuuuuuu主模块中的同一个模块启动worker,也就是说,从命令行用python-m而不是芹菜运行模块。我试过这个: app = Celery('project', include=['project.tasks']) # do all kind of project-specific configuration # that should occur whenever this module is imported if

我已经在一个模块中定义了一个
芹菜
应用程序,现在我想从它的
\uuuuuuuuuuu
主模块中的同一个模块启动worker,也就是说,从命令行用
python-m
而不是
芹菜
运行模块。我试过这个:

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'])
但现在芹菜认为我在毫无争议地管理工人:

Usage: worker <command> [options] 

Show help screen and exit.

Options:
  -A APP, --app=APP     app instance to use (e.g. module.attr_name)
[snip]
但这抱怨了
-A
没有被识别

那我该怎么做呢?或者,如何将回调传递给worker,使其记录有关其配置的信息?

基于此,您可以尝试以下方法:

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)

使用app.worker_main方法(v3.1.12):


我想你只是错过了包装ARG,这样芹菜就可以阅读它们了,比如:

queue = Celery('blah', include=['blah'])
queue.start(argv=['celery', 'worker', '-l', 'info'])
因为事情已经改变了 现在,
worker\u main
将显示以下结果:

AttributeError: 'Celery' object has no attribute 'worker_main'
对于芹菜5,请执行以下操作:
app=芹菜。芹菜(
“项目”,
include=['project.tasks']
)
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
worker=app.worker(
include=['project.tasks']
)
worker.start()

详细信息请参见此处和(希望以后能更好地对其进行记录)。

worker\u main
被放回芹菜5.0.3中:

这在5.0.4上对我起了作用:

self.app.worker\u main(argv=['worker','-loglevel=info','-concurrency={}.format(os.environ['celerry\u concurrency']),'-without gossip']

看起来这是一种不推荐的启动worker的方法,但是他们的新方法在Django代码中太复杂了,我无法理解。我同意这个解决方案,谢谢。为什么不推荐?调用
python manage.py celeryd
时会运行此代码。它不会抛出任何警告。代码说这是运行worker的旧方法。现在我看到了。替代方案具有相同的概念。获取并修改
djcelery/management/commands/celery.py
code,使其始终表现为调用了
/manage.pu芹菜工人
。能否显示myapp模块包含的内容?thanksit可能是这样的:`from celery import celery app=celery()`我得到了这个错误,AttributeError:'celery'对象没有属性'worker\u main'。感谢x100的帮助--我正在试图找出如何从python启动芹菜工人,而不是在命令行中使用芹菜cli
queue = Celery('blah', include=['blah'])
queue.start(argv=['celery', 'worker', '-l', 'info'])
AttributeError: 'Celery' object has no attribute 'worker_main'