捕获芹菜任务发送的django信号 [英] Catch django signal sent from celery task

查看:46
本文介绍了捕获芹菜任务发送的django信号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

捕获从芹菜任务发送的django信号.是否可以?据我所知,它们运行在不同的进程中

Catch django signal sent from celery task. Is it possible? As far as I know they are running in different processes

@celery.task
def my_task():
    ...
    custom_signal.send()

@receiver(custom_signal)
def my_signal_handler():
    ...

推荐答案

请注意,您的异步任务必须是@shared_task装饰器.以便从外部调用,因为它不会附加到具体的应用实例.文档@shared_task芹菜

Please take in mind that your async task must be @shared_task decorator. in order to be called from outside since it will not be attached to a concrete app instance. Documentation @shared_task celery

task.py

@shared_task
def send_email(email):
    # Do logic for sending email or other task

signal.py

正如您在下面看到的那样,这仅在模型Contract的post_save(在用户执行保存之后)时执行(在您的情况下,合同将是正在执行的任何其他模型).

as you can see below this will only execute when post_save (After user executes save) for the model Contract in your case would be anyother model that its being executed.

@receiver(post_save, sender=Contract)
def inflation_signal(sender, **kwargs):
    if kwargs['created']:
        send_email.delay('mymail@example.com')

这篇关于捕获芹菜任务发送的django信号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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