简体   繁体   English

如何运行多个芹菜工人?

[英]How to run multiple celery workers?

I have two tasks in python which want to run them in background by celery.我在 python 中有两个任务想通过 celery 在后台运行它们。 I use the following functions to define the celery.我使用以下函数来定义芹菜。

from celery import Celery
def make_celery(app):
    celery = Celery(
        app.import_name,
        backend=app.config['CELERY_RESULT_BACKEND'],
        broker=app.config['CELERY_BROKER_URL']
    )
    celery.conf.update(app.config)

    class ContextTask(celery.Task):
        def __call__(self, *args, **kwargs):
            with app.app_context():
                return self.run(*args, **kwargs)

    celery.Task = ContextTask
    return celery


flask_app = Flask(__name__)
flask_app.config.update(
    CELERY_BROKER_URL='redis://localhost:6379/0',
    CELERY_RESULT_BACKEND='redis://localhost:6379/0'
)
celery = make_celery(flask_app)



####task 1
@celery.task(name="run1")
def code1(a, b):
    return a * b

####task 2
@celery.task(name="run2")
  def code2(c, d):
      return c + d

 code1.delay(5, 8)
 code2.delay(77, 26)

I run the celery workers as following:我按以下方式运行芹菜工人:

celery -A run1.celery worker&
celery -A run2.celery worker&

But when I send some jobs to the background I face with the errors below:但是,当我将一些作业发送到后台时,我会遇到以下错误:

""Received unregistered task of type 'run1'. ""收到类型为'run1'的未注册任务。 The message has been ignored and discarded.该消息已被忽略并丢弃。 Did you remember to import the module containing this task?你记得导入包含这个任务的模块吗? Or maybe you're using relative imports?""或者也许你正在使用相对进口?""

When I run only one of the celery workers, it works without any error - but when run multiple celery workers I face with this error.当我只运行一个芹菜工人时,它可以正常工作 - 但是当我运行多个芹菜工人时,我会遇到这个错误。

I know I'm way late to this party, but maybe this can help someone else.我知道我参加这个聚会已经很晚了,但也许这可以帮助其他人。

The proper way to start a celery worker:启动芹菜工人的正确方法:

celery -A <filename containing celery object>:<celery object> worker

A standard invocation is:标准调用是:

celery -A tasks:celery worker

Where tasks.py contains the functions and celery object.其中tasks.py包含函数和 celery 对象。 Celery will pickup each task/function in the file automatically. Celery 将自动拾取文件中的每个任务/功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM