How To Perform asynchronous task in django?

2019-08-19 00:12发布

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.

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?

1条回答
ゆ 、 Hurt°
2楼-- · 2019-08-19 00:46

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.

查看更多
登录 后发表回答