django How to execute a function asynchronously i.e, handover a task to a sub process and return response

天大地大妈咪最大 提交于 2021-02-13 05:37:50

问题


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')

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

Note: I am new to django and python.


回答1:


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

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



来源:https://stackoverflow.com/questions/51790935/django-how-to-execute-a-function-asynchronously-i-e-handover-a-task-to-a-sub-pr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!