python - 在烧瓶中使用 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 两次,第二次调用等待第一个首先给出响应。
这里有什么问题?我怎样才能使它异步?
解决方案
我们使用 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
推荐阅读
- ios - FrameLayout IOS 替代方案
- c++ - C++ 对象和命名空间
- javascript - 未创建图表
- apache - 如何让 Twitter 提供压缩的 js?
- python - 数据库数据的字符串编码/解码问题
- python - 如何纠正python填充之间的错误,TypeError:'PolyCollection'对象不可迭代
- angularjs - 如何在 ionic1 codova 项目中集成原生应用程序代码?
- python - Python:如何从类可访问的类方法中创建一个变量?
- vhdl - VHDL:添加两个数字时的边界检查错误
- visual-studio - 如何计算 Visual Studio 解决方案中的所有方法