如果这是重复的,我真的很抱歉,但我一直在寻找,但无法找到答案。
在 Django 中,我想做类似的事情(见评论):
# 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
进程在后台运行(速度很慢)。我认为子进程可能会这样做。
upload_file
不幸的是,上面的代码似乎在将用户重定向到成功页面之前等待进程完成,这不是我需要的。
请注意,这upload_file
是我views.py
文件中的 Python 函数。我听说过popen
——我想我可以转换def upload_file
成一个独立的 Python 脚本并从命令行运行它——它会开始运行然后立即重定向用户,如我所愿吗?还是我应该使用某种线程/进程?
- - 更新 - - -
我想我可能已经弄清楚了……如果我只是做p.start()
而不是p.join()
,那么该过程会立即重定向。不过,这有什么危险吗?