问题
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