Global state of long-running process in django
I have the need to kick off a long-running process in response to a form submission in django. Then, I'd like to be able to poll using ajax and have the view respond with the state of the process (started, stopped, or running). Additionally, I want to be able to stop the process.
so my view looks like this:
def start()
. . .
def stop()
. . .
def status()
. . .
This particular question has been addressed multiple times on S.O., but my situation is subtly different: I am looking for a solution that is entirely pythonic, does not require anything not found in stock django, and开发者_开发问答 does not require the use of a database.
The closest thing I have found is this post. I have implemented the code, and streamlined it quite a bit, but it turns out that request.session is not global (two browsers have different sessions).
It might be that my problem could be solved by using something other than request.session to save the start/stop/status state, but I have no idea what that would be.
Another possibility I came across in my reading is django middleware, but according to this,
__init__() is called only once — at server startup — not for individual requests
There is also a blog post here that talks about global state in django. Any thoughts?
If the state needs to be accessible from any process, and you don't want to store it in the database (and I can't see why not, that's the best place for it), you could store it in a temporary file on the filesystem.
One approach would be to kick off a separate thread and monitor it via the threading module.
You need to use a task queue framework. Try this: http://ask.github.com/celery/index.html
精彩评论