Python 流式服务器在tornado中发送事件
我试图在运行于python 3.5.2上的tornado 6.0.3中实现服务器发送事件(SSE)。我的请求处理程序基本上如下所示:Python 流式服务器在tornado中发送事件,python,python-3.x,asynchronous,tornado,server-sent-events,Python,Python 3.x,Asynchronous,Tornado,Server Sent Events,我试图在运行于python 3.5.2上的tornado 6.0.3中实现服务器发送事件(SSE)。我的请求处理程序基本上如下所示: class SSEHandler(tornado.web.RequestHandler): async def get(self): from tornado.gen import sleep self.set_header('content-type', 'text/event-stream') self.
class SSEHandler(tornado.web.RequestHandler):
async def get(self):
from tornado.gen import sleep
self.set_header('content-type', 'text/event-stream')
self.set_header('cache-control', 'no-cache')
try:
for i in range(60):
await sleep(1)
self.write('data: Test data\n\n')
await self.flush()
except StreamClosedError:
pass
不幸的是,客户端似乎只有在for循环被使用之后才收到响应。我希望处理程序在RequestHandler.flush()上对每条消息进行流式处理。我遗漏了什么?原因可能不在服务器端,而在客户端-您的请求可能已实现,因此只有在接收到整个请求正文并结束请求时才会返回,而这仅发生在全循环消耗时。你试过在那里检查吗?可能原因不在服务器端,而在客户端-你的请求可能会被实现,所以只有当整个请求体被接收并且请求结束时才会返回,这只会发生在全循环消耗时。你试过在那里检查吗?