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

查看:94
本文介绍了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 已经存在很长时间了,它广为人知并被记录在案,但最近我'我将我所有的东西移到 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天全站免登陆