Flask服务器发送事件套接字异常 [英] Flask server sent events socket exception
问题描述
我正在考虑使用SSE将新数据推送到客户端,并使用Flot(javascript图表库)显示实时更新。我的服务器运行在python Flask框架上,我已经想出了如何将数据推送到客户端,但是一旦离开页面就会出现问题:
在处理来自('127.0.0.1',38814)的请求期间发生异常
Traceback(最近一次调用最后一个):
文件/usr/lib/python2.7/ SocketServer.py,第582行,在process_request_thread
self.finish_request(request,client_address)
文件/usr/lib/python2.7/SocketServer.py,行323,在finish_request
self.RequestHandlerClass(request,client_address,self)
文件/usr/lib/python2.7/SocketServer.py,行640,位于__init__
self.finish()
文件/usr/lib/python2.7/SocketServer.py,第693行,完成
self.wfile.flush()
文件/usr/lib/python2.7/socket.py ,第303行,在
self._sock.sendall(view [write_offset:write_offset + buffer_size])
错误:[Errno 32] Broken pipe
我明白为什么e发生错误 - 由于无限循环提供实时数据,套接字永远不会关闭。问题是如何检测页面更改并干净地关闭套接字?我可以关闭客户端的连接吗?那么我怎么检测页面改变呢?
这是服务器代码的骨架,我当然会用包含要显示的对象列表的json替换文本消息: / b>
def event_stream():
导入时间
而真:
time.sleep(1 )
yielddata:这是一个消息号码X.\\\\
@ app.route('/ stream')
def stream():
返回Response(event_stream(),mimetype =text / event-stream)
onBeforeUnload
或者jQuery的 window.unload()
一个Ajax调用一些关闭句柄的拆卸方法。例如:$ b $ pre $ $ $ $($)窗口.unload(
函数(){
$ .ajax(键入:'POST',
async:false,
url:'foo.com/client_teardown')
}
}
onBeforeUnload()和 unload()
/
被处理,所以你可能需要做更多的工作来做类似Chrome的事情。
I am thinking of using SSE to push new data to the client and using Flot(javascript charting library) display "live" updates. My server runs on python Flask framework and I have figured out how to push the data to the client, but the problem occurs as soon as I leave the page:
Exception happened during processing of request from ('127.0.0.1', 38814)
Traceback (most recent call last):
File "/usr/lib/python2.7/SocketServer.py", line 582, in process_request_thread
self.finish_request(request, client_address)
File "/usr/lib/python2.7/SocketServer.py", line 323, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "/usr/lib/python2.7/SocketServer.py", line 640, in __init__
self.finish()
File "/usr/lib/python2.7/SocketServer.py", line 693, in finish
self.wfile.flush()
File "/usr/lib/python2.7/socket.py", line 303, in flush
self._sock.sendall(view[write_offset:write_offset+buffer_size])
error: [Errno 32] Broken pipe
I understand why the error occurs - the socket is never closed due the infinite loop serving up "live" data. Question is how do I detect the page change and cleanly close the socket? Can I close the connection on the client side? How do I detect the page change then?
This is the server code skeleton, I would of course replace the text message with json containing the list of objects to display:
def event_stream():
import time
while True:
time.sleep(1)
yield "data: This is a message number X.\n\n"
@app.route('/stream')
def stream():
return Response(event_stream(), mimetype="text/event-stream")
You could use either onBeforeUnload
or jQuery's window.unload()
to make an Ajax call to some tear down method that closes the handle. Something like:
$(window).unload(
function() {
$.ajax(type: 'POST',
async: false,
url: 'foo.com/client_teardown')
}
}
There are some inconsistencies with how the unload()
/onBeforeUnload()
are handled, so you may have some more work to do in something like Chrome.
这篇关于Flask服务器发送事件套接字异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!