如何覆盖芹菜任务的后端

时间:2022-08-20 19:17:15

we're using Redis as our result backend. However for one task we'd like to override this to use RabbitMQ instead.

我们使用Redis作为结果后端。但是对于一项任务,我们想要覆盖它以使用RabbitMQ。

The documentation for Task.backend says:

Task.backend的文档说:

The result store backend to use for this task. Defaults to the CELERY_RESULT_BACKEND setting

结果存储后端用于此任务。默认为CELERY_RESULT_BACKEND设置

So I had assumed that we could set Task.backend to a string of the same format accepted by CELERY_RESULT_BACKEND.

所以我假设我们可以将Task.backend设置为CELERY_RESULT_BACKEND接受的相同格式的字符串。

So I try this:

所以我试试这个:

celeryconfig.py

CELERY_RESULT_BACKEND = "redis://redis-host:7777"

tasks.py

@app.task(backend='amqp://guest@localhost/tasks-stg')
def my_task(params):
    ...

However the worker fails with :

然而,工人失败了:

[2015-05-07 13:33:49,264: ERROR/Worker-1] Process Worker-1
Traceback (most recent call last):
  File "/project/python2.7_x64/lib/python2.7/site-packages/billiard/process.py", line 292, in _bootstrap
    self.run()
  File "/project/python2.7_x64/lib/python2.7/site-packages/billiard/pool.py", line 286, in run
    self.after_fork()
  File "/project/python2.7_x64/lib/python2.7/site-packages/billiard/pool.py", line 389, in after_fork
    self.initializer(*self.initargs)
  File "/project/python2.7_x64/lib/python2.7/site-packages/celery/concurrency/prefork.py", line 81, in process_initializer
    app=app)
  File "/project/python2.7_x64/lib/python2.7/site-packages/celery/app/trace.py", line 178, in build_tracer
    store_result = backend.store_result
AttributeError: 'str' object has no attribute 'store_result'

1 个解决方案

#1


The documentation is incorrect. Task.backend is actually an instance of a backend class from celery.backends. In this case to override the task class I had to do this:

文档不正确。 Task.backend实际上是来自celery.backends的后端类的实例。在这种情况下,要覆盖任务类,我必须这样做:

from celery.backends.amqp import AMQPBackend

@app.task(backend=AMQPBackend(app, url='amqp://guest@localhost/tasks-stg'))
def my_task(params):
    ...

However the workers continue to use the default class and don't seem to offer a way to override this.

但是,工作人员继续使用默认类,似乎没有提供覆盖它的方法。

#1


The documentation is incorrect. Task.backend is actually an instance of a backend class from celery.backends. In this case to override the task class I had to do this:

文档不正确。 Task.backend实际上是来自celery.backends的后端类的实例。在这种情况下,要覆盖任务类,我必须这样做:

from celery.backends.amqp import AMQPBackend

@app.task(backend=AMQPBackend(app, url='amqp://guest@localhost/tasks-stg'))
def my_task(params):
    ...

However the workers continue to use the default class and don't seem to offer a way to override this.

但是,工作人员继续使用默认类,似乎没有提供覆盖它的方法。