views:

214

answers:

1

I'm really sorry if this is a duplicate, but I have been searching, and haven't been able to find the answer.

In Django I want to do something like (see the comments):

# we have a file in our database...
v = create_file_entry(clean_data, ip_address)
# next, start a *background process* to upload the file somewhere else...    
p = Process(target=upload_file, args=(filepath,v))
p.start()
p.join()
# ...we don't care about the result of the process, or 
# wait for it to finish, just redirect the user to success *immediately*
return HttpResponseRedirect('/success/')

I want the code to start the upload_file process, and then redirect the user to the success page immediately, while the upload_file process runs in the background (it's slow). I thought a subprocess might do this.

Unfortunately the code above seems to wait for the upload_file process to finish before heading before redirecting the user to the success page, which is not what I need.

Note that upload_file is a Python function in my views.py file. I've heard about popen - I guess I could convert def upload_file into a stand-alone Python script and run it from the command line - would that start running and then immediately redirect the user, as I want? Or should I be using some kind of thread/process?

----UPDATE-----

I think I might have figured it out... if I just do p.start() and NOT p.join(), then the process redirects immediately. Is there anything dangerous about this, though?

+3  A: 

Consider writing a separate daemon for the processing and communicating with it via a message queue.

Ignacio Vazquez-Abrams
Thanks, but I don't actually need to communicate with it, or know what the result is. I just need it to start. Surely there must be a simple way to do this?
AP257
...by which I mean, start, and then let me redirect the user WITHOUT waiting for the result. Thread? Process? Popen? Help!
AP257
Put a message on the queue. The daemon then picks it up at its convenience and handles it.
Ignacio Vazquez-Abrams
You are communicating with the daemon by putting a message on the queue, there just will be no communication coming back from the daemon. Think of the message that you put on the queue as your `p = Process(...)` line and the daemon grabbing the message and doing the work as your `p.start` line.
istruble