我尝试设置一个最小的 Flask 应用程序,它使用eventlet来立即响应并发请求,而不是阻塞和响应一个又一个请求(就像标准的 Flask 调试网络服务器所做的那样)。
先决条件:
pip install Flask
pip install eventlet
根据我到目前为止在互联网上发现的内容的理解,它应该像这样工作:
# activate eventlet
import eventlet
eventlet.monkey_patch()
from flask import Flask
import datetime
from time import sleep
# create a new Flask application
app = Flask(__name__)
# a short running task that returns immediately
@app.route('/shortTask')
def short_running_task():
start = datetime.datetime.now()
return 'Started at {0}, returned at {1}'.format(start, datetime.datetime.now())
# a long running tasks that returns after 30s
@app.route('/longTask')
def long_running_task():
start = datetime.datetime.now()
sleep(30)
return 'Started at {0}, returned at {1}'.format(start, datetime.datetime.now())
# run the webserver
if __name__ == '__main__':
app.run(debug=True)
运行此文件,然后http://localhost:5000/longTask
在 webbrowser 选项卡中打开时,当它仍在处理打开另一个选项卡时http://localhost:5000/shortTask
,我希望第二个选项卡在第一个选项卡仍在加载时立即返回。但是,与在标准 Werkzeug 服务器上运行此程序类似,第二个选项卡仅在第一个选项卡在 30 秒后完成后才返回。
这里有什么问题?顺便说一句,鉴于预期只有少数并发用户(最多 5 个),这是否是通常所说的 Flask 的“生产就绪网络服务器”?
顺便说一句,当我使用Flask-socketio 库运行网络服务器时,根据文档,如果安装了它,它会自动选择 eventlet,然后它按预期工作。
Flask-socketio 的完整示例:
# activate eventlet
import eventlet
eventlet.monkey_patch()
from flask import Flask
from flask_socketio import SocketIO
import datetime
from time import sleep
# create a new Flask application
app = Flask(__name__)
# activate Flask-socketio
socketio = SocketIO(app)
# a short running task that returns immediately
@app.route('/shortTask')
def short_running_task():
start = datetime.datetime.now()
return 'Started at {0}, returned at {1}'.format(start, datetime.datetime.now())
# a long running tasks that returns after 30s
@app.route('/longTask')
def long_running_task():
start = datetime.datetime.now()
sleep(30)
return 'Started at {0}, returned at {1}'.format(start, datetime.datetime.now())
# run the webserver with socketio
if __name__ == '__main__':
socketio.run(app, debug=True)