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?

Community
  • 1
  • 1
  • So the problem with what @jetxee posted is that you want anyone (regardless of who started the long-running task) to be able to check the progress of that task? – Jack M. Jan 29 '10 at 20:36
  • If you say "does not require anything not found in stock django", and then say "does not require the use of a database." you've taken a large chunk of stock django out of the equation, given Django's coupling to its database. Care to enlighten us why? – Spacedman Oct 22 '11 at 10:41

3 Answers3

1

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

Andrew Sledge
  • 10,163
  • 2
  • 29
  • 30
1

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
  • 588,541
  • 66
  • 880
  • 895
  • That's what I ended up doing. I used a file with known state /tmp/program.pid –  Jan 29 '10 at 21:55
-1

You need to use a task queue framework. Try this: http://ask.github.com/celery/index.html

qubird
  • 529
  • 1
  • 6
  • 4