Django subprocess

2019-07-20 10:57发布

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?

1条回答
Anthone
2楼-- · 2019-07-20 11:13

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

查看更多
登录 后发表回答