TheMeaningfulEngineer
TheMeaningfulEngineer

Reputation: 16329

Simultaneous multitasking in Django

I have in my web project a time consuming function. While the function is doing its computations, a web page should be rendered informing the user that the results will be sent by email once the computation is done.

If I put the rendering after the function call, the web page wont be rendered until after the time_consuming_function() had finished and that would make the response senseless.

views.py:

def web_function(request):
    ...
    time_consuming_function()
    return HttpResponse()

Is python threading the only way to go?


Update

Ended up using cellery, since it seemed better documented than ztaskd

Upvotes: 1

Views: 1189

Answers (2)

vartec
vartec

Reputation: 134551

The way to go is to use ztaskd to execute time_consuming_function().

from django_ztask.decorators import task

@task()
def time_consuming_function()
    ...

views.py:

def web_function(request):
    ...
    time_consuming_function.async()
    return HttpResponse()

Upvotes: 6

Steve Mayne
Steve Mayne

Reputation: 22808

I suggest you look at a task-scheduling framework, such as Celery (or just plain Rabbit MQ)

Upvotes: 4

Related Questions