Marc
Marc

Reputation: 3596

execute javascript after long running Django task

Suppose I have a Django website. On one page my function in views.py has to execute a process that may take 5-15 seconds to execute. Obviously I don't want to wait for this process to complete before rendering the page. The problem is that some of the Javascript on the page needs to use the results of the long running process. So how can I execute this long running process (say in a python thread or Deferred) and somehow get the results to Javascript after the page is loaded?

Upvotes: 0

Views: 197

Answers (1)

Tudmotu
Tudmotu

Reputation: 905

You have two different methods you can use, depending on the browsers you need to support.

If you need to support IE, you need to use a method call "Long-Polling". Long polling is basically a deferred ajax request, which means that once the server gets that request, it keeps it hanging until he can give it the result of the operation (or terminates it on timeout, and then the client needs to send another long-poll request). I personally view this as a hack over traditional XHR.

The preferred, but less supported method, is what's called Server Sent Events.
Server Sent Events are a way for the server to notify the client about an event at any point, without the client needing to first send a request.

For further details, see this extensive answer.

Upvotes: 0

Related Questions