python - 如何在 Flask Python 的 WSGIServer 中启用/实现多线程
问题描述
我有一个用于 Web 和移动应用程序的 Flask API。但有时在重负载时,应用程序或网站会停止快速响应并显示结果需要时间, 我只想在运行 WSGIServer 的烧瓶中启用多线程。
def main():
"""Main entry point of the app."""
try:
http_server = WSGIServer(('0.0.0.0', 8084), app, log=logging, error_log=logging)
http_server.serve_forever()
except Exception as exc:
logger.error(exc.message)
logger.exception(traceback.format_exc())
finally:
# Do something here
pass
谢谢,
解决方案
The built-in Flask development server, whilst not intended for multithreaded use or deployment does allow multithreading:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def index():
return 'Hello, world!'
if __name__ == '__main__':
app.run(threaded=True)
The above code is a simple Hello World script that uses multithreading; not that any process is using another thread, but you get the idea.
推荐阅读
- django - 注册时用户看不到 Django 中的消息错误
- excel - 优化 excel/google 表格中的销售问题
- python - Numpy.dot 给出相同的形状 (X, Y) (X,Y) 未对齐
- list - 将表中的新列组合到另一个表的函数
- python - 有没有办法替换 for 循环中的变量?
- datajoint - 调用 .alter() 时出现 NotImplementedError (0.13.1)
- apache-spark - 用最大行值替换 PySpark Dataframe 组中的值
- python - 我如何编写一个 python 代码来将行中的每个元素相乘?
- c# - WSO2 身份服务器 5.7.0 撤销方法不起作用
- python - Django不更新静态文件,清除浏览器缓存不起作用