尽管有Monkey.patch_all(),调用内置的 open()或 pandas.read_csv()似乎也会阻止对我的烧瓶+ gevent wsgi网络服务器的所有其他请求。我是否需要调用特殊的gevent io函数以使其不阻塞?
from gevent import monkey, pywsgi, sleep
monkey.patch_all()
import pandas as pd
from flask import Flask
app = Flask(__name__)
FILENAME = 'c:/temp/testcsv.csv'
@app.route('/')
def fastresult():
return 'this should return immediately'
@app.route('/non_blocking_sleep')
def non_blocking_sleep():
sleep(10)
return 'using gevent.sleep does NOT block other reqeusts as expected'
@app.route('/readcsv')
def readcsv():
"""
this blocks any other request before the read completes
"""
df = pd.read_csv(FILENAME)
return df.info()
@app.route('/openfile')
def openfile():
"""
this blocks any other request before the read completes
"""
with open(FILENAME, 'r') as file:
res = file.readlines()
return res[:1000]
http_server = pywsgi.WSGIServer(('', 5000), app)
http_server.serve_forever()
在Windows和Linux上的64位Anaconda 5.3(python 3.7)上进行了测试。