django如何异步执行功能,即将任务移交给子进程并返回响应 [英] django How to execute a function asynchronously i.e, handover a task to a sub process and return response

查看:56
本文介绍了django如何异步执行功能,即将任务移交给子进程并返回响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用django 2.0和python 3.6.

I am using django 2.0 and python 3.6.

用户注册包括发送验证邮件.而且此邮件发送过程需要更长的时间,并且用户一直在等待.

The user registration includes sending of a verification mail. And this mail sending process is taking longer and the user is kept waiting.

我需要什么?:如果用户注册表格有效,则邮件详细信息将发送到另一个任务处理程序,并且无论是否发送邮件,该函数都必须恢复并返回响应.

What I need?: If the user registration form is valid, mailing details are sent to another task handler and regardless of whether the mail is sent or not, the function must resume and return a response.

def new_user_registration(request):
    form = CustomUserCreationForm(request.POST or None)
    if form.is_valid():
        user = form.save()
        send_verification_mail(user) #<= taking more time.
        return render(request, 'registration/signup.html')

即,必须异步调用 send_verification_mail .如何实现?

i.e, the send_verification_mail must be called asynchronously. How to achieve it?

注意::我是django和python的新手.

Note: I am new to django and python.

推荐答案

大多数时候,您将使用

Most of the time you will use a task queue for this.

Celery 已经存在了很长时间,并且众所周知并且有记载,但是最近我m将我所有的东西移动到 TaskTiger

Celery has been around for a long time and it is well known and documented, but recently I'm moving all my stuff to TaskTiger

这篇关于django如何异步执行功能,即将任务移交给子进程并返回响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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