使用 Flask 在 Python 中进行长轮询
Long Polling in Python with Flask
我正在尝试在 Flask 框架下使用 JQuery 和 Python 进行长轮询。
之前在 PHP 中做过长轮询,我尝试以同样的方式进行:
一个具有 while(true) 循环的 script/function,定期检查数据库中的更改 eg.every 0.5 秒,并在发生更改时 returns 一些数据。
所以在我的 ini.py 中,我创建了一个 app.route 到 /poll 供 JQuery 调用。 JQuery 向它提供有关客户端当前状态的一些信息,poll() 函数将此信息与数据库中的当前信息进行比较。当观察到变化时,循环结束并 returns 信息。
这是 python 代码:
@app.route('/poll')
def poll():
client_state = request.args.get("state")
#remove html encoding + whitesapce from client state
html_parser = HTMLParser.HTMLParser()
client_state = html_parser.unescape(client_state)
client_state = "".join(client_state.split())
#poll the database
while True:
time.sleep(0.5)
data = get_data()
json_state = to_json(data)
json_state = "".join(data) #remove whitespace
if json_state != client_state:
return "CHANGE"
问题是,当上面的代码开始轮询时,服务器似乎超载了,其他 Ajax 调用,以及其他请求,例如将 "loading" 图片加载到 html 使用 JQuery 无响应和超时。
为了完整起见,我在此处包含了 JQuery:
function poll() {
queryString = "state="+JSON.stringify(currentState);
$.ajax({
url:"/poll",
data: queryString,
timeout: 60000,
success: function(data) {
console.log(data);
if(currentState == null) {
currentState = JSON.parse(data);
}
else {
console.log("A change has occurred");
}
poll();
},
error: function(jqXHR, textStatus, errorThrown) {
console.log(jqXHR.status + "," + textStatus + ", " + errorThrown);
poll();
}
});
}
这个需要多线程什么的吗?或者有人知道我为什么会遇到这种行为吗?
提前致谢!! :)
正如 link @Robᵩ 提到的那样,你的烧瓶应用程序只是过载。这是因为 运行 app.run()
时,flask 应用程序默认处于单线程模式,因此它每次只能处理一个请求。
您可以通过以下方式启动多线程:
if __name__ == '__main__':
app.run(threaded=True)
或者使用像 gunicorn 或 uwsgi 这样的 WSGI 服务器来为 flask 提供多重处理:
gunicorn -w 4 myapp:app
希望您喜欢 Python 和 Flask!
我正在尝试在 Flask 框架下使用 JQuery 和 Python 进行长轮询。
之前在 PHP 中做过长轮询,我尝试以同样的方式进行:
一个具有 while(true) 循环的 script/function,定期检查数据库中的更改 eg.every 0.5 秒,并在发生更改时 returns 一些数据。
所以在我的 ini.py 中,我创建了一个 app.route 到 /poll 供 JQuery 调用。 JQuery 向它提供有关客户端当前状态的一些信息,poll() 函数将此信息与数据库中的当前信息进行比较。当观察到变化时,循环结束并 returns 信息。
这是 python 代码:
@app.route('/poll')
def poll():
client_state = request.args.get("state")
#remove html encoding + whitesapce from client state
html_parser = HTMLParser.HTMLParser()
client_state = html_parser.unescape(client_state)
client_state = "".join(client_state.split())
#poll the database
while True:
time.sleep(0.5)
data = get_data()
json_state = to_json(data)
json_state = "".join(data) #remove whitespace
if json_state != client_state:
return "CHANGE"
问题是,当上面的代码开始轮询时,服务器似乎超载了,其他 Ajax 调用,以及其他请求,例如将 "loading" 图片加载到 html 使用 JQuery 无响应和超时。
为了完整起见,我在此处包含了 JQuery:
function poll() {
queryString = "state="+JSON.stringify(currentState);
$.ajax({
url:"/poll",
data: queryString,
timeout: 60000,
success: function(data) {
console.log(data);
if(currentState == null) {
currentState = JSON.parse(data);
}
else {
console.log("A change has occurred");
}
poll();
},
error: function(jqXHR, textStatus, errorThrown) {
console.log(jqXHR.status + "," + textStatus + ", " + errorThrown);
poll();
}
});
}
这个需要多线程什么的吗?或者有人知道我为什么会遇到这种行为吗?
提前致谢!! :)
正如 link @Robᵩ 提到的那样,你的烧瓶应用程序只是过载。这是因为 运行 app.run()
时,flask 应用程序默认处于单线程模式,因此它每次只能处理一个请求。
您可以通过以下方式启动多线程:
if __name__ == '__main__':
app.run(threaded=True)
或者使用像 gunicorn 或 uwsgi 这样的 WSGI 服务器来为 flask 提供多重处理:
gunicorn -w 4 myapp:app
希望您喜欢 Python 和 Flask!