2

this is a different aproach for the question I already asked

Simultaneous multitasking in Django

so it will share the intro.


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.

views.py:

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

Since the page that has to be rendered is quite simple, and it requires no return information from time_consuming_function() is there a way to skip multitasking and just somehow first render and show the page and than call time_consuming_function()?


Possible solution:

  1. When the input button is pressed on the page that precedes the calling of "web_function", attach some javascript code which will render the temporary page?

Update

Ended up using celery. Wasn't successful in experimenting with Ajax.

1

1 Answer 1

7

A return statement in a function represents the end of that function.* Your snippet's time_consuming function is outside the function's scope and thus will never be reached.

A solution for handling the task could be: celery which is well suited for wrapping functions as asynchronous tasks. If you must do things synchronous, why don't you use ajax for this so you can atleast show the user what's going on with a BeforeSend?

*This is true for Python and most, if not all, object oriented programming languages

Sign up to request clarification or add additional context in comments.

4 Comments

+1, in fact, if you have a smart IDE it will tell you that "code is not reachable".
+1 for using celery. However, in general don't perform long running tasks in your Django web processes, even using AJAX (as that is asynchronous for the client but not the Django server), as this will reduce the scalability of your application by starving out available request-processing threads.
@Hedde Can you point to a good tutorial for integrating BeforeSend?
@Alan I have answered another question with an example before you can find it here: stackoverflow.com/questions/11615901/django-api-requests/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.