1

我有一个用于 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

谢谢,

4

1 回答 1

2

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.

于 2018-05-17T18:24:26.423 回答