我需要做一些阻塞性的CPU工作,因此我启动了一个新的aioprocessing AioProcess并使用aioprocessing AioPipe与之通信。
启动过程如下:
async def launch_worker(app):
pipe_a, pipe_b = AioPipe()
process = AioProcess(target=worker, args=(pipe_b,))
process.start()
app.process, app.pipe = process, pipe_a
await sleep(0)
启动时启动工作程序时,一切正常。但是,如果我从某个角度启动该工作人员,则最终会出现一种奇怪的行为。具体来说,当我尝试使用ctrl-c KeyboardInterrupt关闭服务器时,服务器挂起了–它停止处理新请求,但直到我执行操作系统终止操作后才终止。
这是完整的脚本:
from aiohttp import web
import logging
from logging.config import dictConfig
from aioprocessing import AioPipe, AioProcess
from asyncio import shield, sleep, get_event_loop
import uuid
import time
async def the_view(request):
msg = str(uuid.uuid4())[0:8]
if request.app.process is None:
logging.info("Launching worker process in view.")
await launch_worker(request.app)
pipe = request.app.pipe
logging.info("Webserver Sending {}".format(msg))
pipe.send(msg)
output = await shield(pipe.coro_recv())
logging.info("Webserver received {}".format(output))
return web.Response(text=output)
def worker(pipe):
try:
while True:
foo = pipe.recv()
if foo is None:
break
else:
time.sleep(3)
msg = str(uuid.uuid4())[0:8]
logging.info("Worker returning {} {}".format(foo, msg))
pipe.send("{} {}".format(foo, msg))
except KeyboardInterrupt:
pipe.send("Worker killed, argggghhhh")
async def launch_worker_at_start(app):
logging.info("Launching worker at start")
return await launch_worker(app)
async def launch_worker(app):
pipe_a, pipe_b = AioPipe()
process = AioProcess(target=worker, args=(pipe_b,))
process.start()
app.process, app.pipe = process, pipe_a
await sleep(0)
def build_app(loop):
app = web.Application(debug=True)
app.process, app.pipe = None, None
app.router.add_get('/', the_view)
## uncomment the following line and everything works ##
# loop.create_task(launch_worker_at_start(app))
#######################################################
return app
if __name__ == '__main__':
logging.info("Webserver starting")
loop = get_event_loop()
this_app = build_app(loop)
web.run_app(this_app, port=8081, loop=loop)
我正在使用aiohttp == 2.3.10和aioprocessing == 0.0.1
编辑:
与aiohttp == 3.4.4和aioprocessing == 1.0.1和python 3.5.6相同的行为。