Show a message 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")


def my_func(request):
    #Some function that takes 10 second and the user should not wait for this
    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 directly (since it all runs synchronous I can’t think of a way), or what would be the most optimal way ?

Correct, there’s not.

There’s not even going to be an indirect way of doing it unless you construct your entire app as an SPA, or add some JavaScript to every page to check the server for notifications.

Keep in mind that every new page removes/replaces _everything from the old page, including any JavaScript that may have been running. So if you render a complete new page, you’ll need to reload some JavaScript to issue a request to the server to see if there are any notifications to be processed. (If they’re going to be on one page for a while, you could use a polling mechanism to see if there are new notifications to be handled while they’re staying on that page.)

1 Like

Yeah, thats what I thought as well… /:

But its gonna pe a performance issue I would assume, if I each second need to a server to get new requests

Really depends upon how many concurrent users you’re expecting - and being realistic about that estimate.
A couple hundred users probably wouldn’t be much sweat - and keep in mind that the polling only needs to occur when someone is sitting on a page and not browsing to other pages. If they’re actively browsing, they’re generating far more load on the server than if they’re just sitting and reading.