Django view and separate processes
I would like to do something similar:
f(n) calculates n! , this obviously takes a long time to do, so the calculations need to run in a separate process from the django view. Additionally I would like the view to return a response immediately (ex. progress 0% ) and subsequent polling needs to update progress, so the view needs to communicate with the开发者_Go百科 above process.
What would be the best way to achieve this?
Try django-celery
Andrey Fedoseev gave a great suggestion, but let me come up with a more general solution. You can create some WaitingTasks model into which where your view puts new tasks. Then, there can use any method to process those waiting tasks - cronjob, upstart daemon, whatever - writing back progress and result.
(In fact celery
uses a similar approach, only with RabbitMQ)
I have used the strategy contained in this link to great affect: Signals in Django. The section on "Handling Signals Asynchronously" where the python threading module is used is what I used.
The signals code is pretty out of date (even though it is at the top of a Google search). Still, the threading code is probably what may help you the most.
精彩评论