Wednesday, March 10, 2010

Django subprocess

Programmer Question

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?



Find the answer here

No comments:

Post a Comment

LinkWithin

Related Posts with Thumbnails