我正在构建烧瓶应用程序,并且需要一些后台进程才能运行。我决定使用多进程,但在Flask中运行时会产生两个进程。有谁知道为什么会发生?我已经在OS X和Ubuntu 12.04上测试过它,结果相同。这里有一个例子:烧瓶应用程序中的多进程启动2个进程
import time
import multiprocessing
from flask import Flask
app = Flask(__name__)
backProc = None
def testFun():
print('Starting')
while True:
time.sleep(3)
print('looping')
time.sleep(3)
print('3 Seconds Later')
@app.route('/')
def root():
return 'Started a background process with PID ' + str(backProc.pid) + " is running: " + str(backProc.is_alive())
@app.route('/kill')
def kill():
backProc.terminate()
return 'killed: ' + str(backProc.pid)
@app.route('/kill_all')
def kill_all():
proc = multiprocessing.active_children()
for p in proc:
p.terminate()
return 'killed all'
@app.route('/active')
def active():
proc = multiprocessing.active_children()
arr = []
for p in proc:
print(p.pid)
arr.append(p.pid)
return str(arr)
@app.route('/start')
def start():
global backProc
backProc = multiprocessing.Process(target=testFun, args=(), daemon=True)
backProc.start()
return 'started: ' + str(backProc.pid)
if __name__ == '__main__':
app.run(port=int("7879"))
我想使用python 2.7.x作为守护进程运行Process来进行异步处理。问题是,即使守护进程设置为True,在进程完成运行之前,端点中的返回也不会发生。我在uWSGI上运行这个,也许这就是为什么。 – radtek