无法通过芹菜信号连接到芹菜任务? [英] Unable to connect to celery task from a celery signal?

查看:90
本文介绍了无法通过芹菜信号连接到芹菜任务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 task_success 信号连接 task2

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

如果删除task2的 app.task 装饰器,则效果很好.但是为什么它无法连接到芹菜任务?

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

推荐答案

技术细节是,该任务将首先由celery worker进行懒惰评估.也就是说,要创建 PromiseProxy 对象而不是 celery.app.task: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

默认情况下, signal.connect()将尝试使用对接收器对象的弱引用[此处为[ PromiseProxy ].这就是为什么您会收到这样的错误.

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.

解决方案非常简单,只需将 connect()参数更改为 False

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

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

但是我发现它只能在Windows上运行.

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'

这篇关于无法通过芹菜信号连接到芹菜任务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆