Unable to connect to celery task from a celery signal?

被刻印的时光 ゝ 提交于 2019-12-24 02:33:25

问题


I am trying to connect task2 from task_success signal

from celery.signals import task_success
from  celery  import  Celery

app  =  Celery()

@app.task
def task1():
    return 't1'

@app.task
def task2():
    return 't2'

task_success.connect(task2, sender=task1)

When I run this code, its throwing

TypeError: cannot create weak reference to 'PromiseProxy' object

If remove app.task decorator for task2, it works perfectly. But why is it unable to connect to celery task?


回答1:


The technical details is that the task will be lazy evaluated by celery worker at first. That is, to create an object of PromiseProxy instead of celery.app.task:Task for performance

And by default, signal.connect() will attempt to use weak references to the receiver objects [Here, it is [PromiseProxy]. This is why you got such error.

The solution is quite simple, just change the weak parameter of connect() to False

task_success.connect(task2, sender=task1, weak=False)

But I found that it only works on windows.

The following one should be okay. To make sure that task decorator is applied last when using multiple decorators in combination with the task decorator

@app.task
@signals.task_success.connect(sender=task1)
def task2():
    return 't2'


来源:https://stackoverflow.com/questions/26284374/unable-to-connect-to-celery-task-from-a-celery-signal

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!