Django子流程 [英] Django subprocess
问题描述
如果这是重复的内容,我真的很抱歉,但是我一直在搜索,但是没有找到答案.
I'm really sorry if this is a duplicate, but I have been searching, and haven't been able to find the answer.
在Django中,我想做类似的事情(请参阅注释):
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/')
我希望代码启动upload_file进程,然后将用户重定向到成功页面立即,而upload_file
进程在后台运行(速度很慢).我以为子进程可以做到这一点.
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.
不幸的是,上面的代码似乎要等到upload_file
进程完成之后,才能将用户重定向到成功页面,这不是我所需要的.
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.
请注意,upload_file
是我的views.py
文件中的Python函数.我听说过popen
-我想我可以将def upload_file
转换为独立的Python脚本并从命令行运行它-是否可以根据需要开始运行然后立即重定向用户?还是应该使用某种线程/进程?
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 -----
我想我可能已经想通了...如果我只是执行p.start()
而不是p.join()
,那么该过程将立即重定向.对此有什么危险吗?
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?
推荐答案
考虑编写一个单独的守护进程进行处理,并通过消息队列与之通信.
Consider writing a separate daemon for the processing and communicating with it via a message queue.
这篇关于Django子流程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!