简体   繁体   中英

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

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

我建议您看一下任务计划框架,例如Celery(或仅是简单的Rabbit MQ

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM