我需要在请求期间执行 Process,如下所示
@app.route('/test')
def test_process():
print "starting new process"
p = Process(target=do_long_extra_job)
p.start()
return "this is response"
do_long_extra_job 在另一个进程上,所以预期的工作流程是这样的
- 启动一个进程
- response
- 长时间运行的额外工作已完成
但实际流程是这样的
- 统计一个进程
- 长时间运行的额外工作已完成
- response
开始新流程后如何立即响应?
The 应用异步 https://docs.python.org/3/library/multiprocessing.html#multiprocessing.pool.Pool.apply_async方法中的多处理池 https://docs.python.org/3/library/multiprocessing.html#using-a-pool-of-workers课程可能适合你。在这种情况下,“Process”对您不起作用,因为它是一种阻塞方法,这意味着您的 return 语句在 p.start() 完成执行之前不会执行。
apply_async 和 multiprocessing.Pool 的示例:
def do_long_extra_job():
#some extra long process
return
@app.route('/test')
def test_process():
print "starting new process"
pool = multiprocessing.Pool(processes=1)
pool.apply_async(do_long_extra_job)
return "this is response"
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)