views:

186

answers:

2

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?

TIA, Noah

A: 

One approach would be to kick off a separate thread and monitor it via the threading module.

Andrew Sledge
+1  A: 

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.

Daniel Roseman
That's what I ended up doing. I used a file with known state /tmp/program.pid
Noah