相关代码:
...
from concurrent.futures import ProcessPoolExecutor
executor = ProcessPoolExecutor(2)
def copy_use_pool():
...
一个耗时很长的任务
...
@app.route('/copy')
def copy():
executor.submit(copy_use_pool)
time.sleep(0.5)
return redirect(url_for('index'))
如代码所示,请求copy
接口时,使用ProcessPoolExecutor
提交一个长耗时的copy_use_pool
,这样确实实现了异步,但每请求一次,后台的进程就增加一个,copy
完成了不退出。
首先,你的app是不是以多进程方式run起来的?