首页 > 解决方案 > 在烧瓶中使用 Gevent:API 不是异步的

问题描述

早些时候我使用的是女服务员。现在我正在使用Gevent运行我的 Flask 应用程序,它只有一个 API

from flask import Flask, request, jsonify
import documentUtil
from gevent.pywsgi import WSGIServer

app = Flask(__name__)

@app.route('/post-document-string', methods=['POST']) 
def parse_data():
    req_data = request.get_json(force=True)
    text = req_data['text']
    result = documentUtil.parse(text)
    return jsonify(keywords = result)

if __name__=='__main__':
    http_server = WSGIServer(('127.0.0.1', 8000), app)
    http_server.serve_forever()

这工作正常。但是 API 不是异步的。如果从前端,我同时触发相同的 API 两次,第二次调用等待第一个首先给出响应。

这里有什么问题?我怎样才能使它异步?

标签: pythonmultithreadingasynchronousflaskwaitress

解决方案


我们使用 Gunicorn 在多个进程中运行 Flask。你可以通过这种方式从 python 中获得更多的汁液 + 自动重启等等。示例配置文件:

import multiprocessing

bind = "0.0.0.0:80"
workers = (multiprocessing.cpu_count() * 2) + 1
# ... additional config

然后用类似的东西运行

gunicorn --config /path/to/file application.app

推荐阅读