简体   繁体   中英

Unable to connect to celery task from a celery signal?

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?

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'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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