django-celery:bind=True 失败,需要 2 个位置参数,但给出了 3 个

问题描述 投票:0回答:1

如果失败,我正在尝试重试芹菜任务。这就是我的任务,

@shared_task(queue='es', bind=True, max_retries=3)
def update_station_es_index(doc_id, doc_body):
    """
    Celery consumer method for updating ES.
    """
    try:
        #es_client is connecting to ElasticSearch
        es_client.update_stations(id=doc_id, body=doc_body)
    except Exception as e:
        self.retry(exc=e)

但是当调用此任务时我收到此错误,

TypeError: update_station_es_index() takes 2 positional arguments but 3 were given

我在网上没有找到足够的帮助来解决这个错误,只是这个github问题,但这并不能解释太多。

谁能告诉我发生了什么事以及解决方案是什么?

使用Django2.0.5和celery4.2.0

python django django-celery
1个回答
27
投票

您必须添加

self
作为参数。当您指定
bind=True
时,任务本身将作为第一个参数传递。

假设您有一个标准任务

add
,它需要两个参数

@shared_task
def add(x, y):
    return x + y

如果您在此任务上指定

bind=True
,它将需要接受另一个参数。

@shared_task(bind=True)
def add(self, x, y):
    # ...

所以改变

def update_station_es_index(doc_id, doc_body):

def update_station_es_index(self, doc_id, doc_body):

另请参阅相关:celery中的bind = True关键字是什么意思?

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.