2015-12-21 66 views
5

我正在使用一个Flask Web服务器来提供一个耗时的计算接口。为了提高性能,我想异步子进程使用flask-aiohttp

  1. 开始计算为new subprocess能够使用多个CPU内核的多个并行计算
  2. 让计算运行的异步使用asyncio

要调用asyncio来自Flask的协程我开始使用flask-aiohttp,它很适合简单的延迟任务,如示例中所示。 然而,我无法从内部瓶调用异步子:

#!/usr/bin/env python3 
# coding: utf-8 

from flask import Flask 
from flask.ext.aiohttp import AioHTTP, async 

import asyncio 
from asyncio.subprocess import PIPE 

CALC_SCRIPT = './calc' 

app = Flask(__name__) 
aio = AioHTTP(app) 

@app.route('/calc/<int:n>') 
@async 
def calc(n): 
    print('calc({}) called'.format(n)) 
    create = asyncio.create_subprocess_exec(CALC_SCRIPT, str(n), 
              stdout=PIPE, stderr=PIPE) 
    print('create... ', end='') 
    process = yield from create 
    print('process created. {!r}, type={}'.format(process, 
                type(process))) 
    yield from process.wait() 
    print('process finished.') 

    # yields (stdout, stderr) 
    result = '\n'.join(ch.decode().rstrip() for ch in 
         (yield from process.communicate()) if ch) 
    return result 

if __name__ == '__main__': 
    aio.run(app, debug=True) 

进程正创建,但不会返回:

GET http://127.0.0.1:5000/calc/5 
calc(5) called 
creating... process created. <Process 5647>, 
    type=<class 'asyncio.subprocess.Process'> 

我在做什么错?

+0

我相信你已经想通了,但* CALC_SCRIPT *做什么?你正在用一个简单的函数进行测试,比如一个阶乘函数或其他东西? – wgwz

+0

这可能会有所帮助:http://stackoverflow.com/questions/24541192/python-asyncio-subprocess-never-finishes – wgwz

+0

是的,'CALC_SCRIPT'是一个[缓慢的斐波纳契脚本](https://gitlab.com/snippets/11746)用于测试目的。我偶然发现了你已经链接到的问题,但我的[独立版本](https://gitlab.com/snippets/11748)运行良好。 – Finwood

回答

1

原因:从子线程运行asyncio子进程有限制,请参阅asyncio文档Subprocess and threads

细节:与debug=True,Flask-aiohttp处理由Werkzeug的run_with_reloader启动的子线程中的请求。关闭调试并且代码正确运行。

或者根据上面的文档,Flask-aiohttp在调用run_with_reloader之前应该添加对asyncio.get_child_watcher()的调用。通过此调用,您的代码即使在debug=True的情况下也能运行。