如何在Django中执行异步任务? [英] How To Perform asynchronous task in django?

查看:67
本文介绍了如何在Django中执行异步任务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里有点迷路了.我想在帖子请求中使用django发送电子邮件.但是我不希望用户等待答复,所以我在后台线程中发送它.电子邮件也应按用户提供的顺序每30秒发送一次.

I am a bit lost here. I want to send email using django on a post request. But I don't want the user to wait for a reply so I am sending it in a background thread. Also the emails are to be send every 30 seconds in a sequential order provided by the user.

但是用户可以选择取消发送电子邮件,即使用不同的帖子请求来处理取消输入.

But there is a option for the user to cancel sending the emails i.e a different post request to handle the cancel input.

如何在发送电子邮件的线程中侦听取消操作?如何实施30秒延迟?我曾考虑过使用睡眠,但是如果这样做,它会跳过取消信号吗?

How do I listen for cancel operation in the thread sending the email? How do I implement the 30 second delay? I thought of using sleep but if I do that then will it skip the cancel signal?

推荐答案

如上所述,可能是使用Celery任务异步发送电子邮件的情况.然后,对于取消电子邮件发送,您处理POST(用于取消)请求的视图可以撤消负责发送该电子邮件序列的任务.此问题/答案显示了操作方法它.

As mentioned above, it would probably be the case of using a Celery task for sending the e-mails asynchronously. Then, for cancelling the e-mails sending, your view handling the POST (for cancelling) request could revoke the tasks responsible for sending that sequence of e-mails. This question/answer shows how to do it.

这篇关于如何在Django中执行异步任务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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