首页 > 解决方案 > Show a message in Django after a function completes, without having the user wait for it to complete

问题描述

Say I have this view:

import threading

def MyView(request):

    thr = threading.Thread(target=my_func, args=(request,)) 
    thr.start()#Runs a function that the user should not wait for
    return render(request, "my_site")

where


def my_func(request):
    #Some function that takes 10 second and the user should not wait for this
    sleep(10)
    messages.success(request, "Your meal is ready!") 
    return render(request, "my_other_site")

What I want to do is; I have a function of which the user should not wait for to complete i.e they should be able to continue browsing, and then when the function is done, I want to show them a message either using messages or a pop-up box or something third.

Is there a way to do that in Django (since it all runs synchronous I can't think of a way), or what would be the most optimal way (

标签: pythondjango

解决方案


推荐阅读