Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 服务器发送了带有Flask和Tornado的事件_Python_Flask_Tornado_Server Sent Events - Fatal编程技术网

Python 服务器发送了带有Flask和Tornado的事件

Python 服务器发送了带有Flask和Tornado的事件,python,flask,tornado,server-sent-events,Python,Flask,Tornado,Server Sent Events,我一直在玩用Flask和Tornado发送服务器发送的事件。我看了这篇博客文章: 我决定尝试编写自己的Flask应用程序,将服务器发送的事件作为练习发送。以下是我名为sse_server.py的Flask应用程序的代码: #! /usr/bin/python from flask import Flask, request, Response, render_template from tornado.wsgi import WSGIContainer from tornado.https

我一直在玩用Flask和Tornado发送服务器发送的事件。我看了这篇博客文章:

我决定尝试编写自己的Flask应用程序,将服务器发送的事件作为练习发送。以下是我名为sse_server.py的Flask应用程序的代码:

#! /usr/bin/python

from flask import Flask, request, Response, render_template

from tornado.wsgi import WSGIContainer
from tornado.httpserver import HTTPServer
from tornado.ioloop import IOLoop

app = Flask(__name__)

def event_stream():
  count = 0
  while True:
    print 'data: {0}\n\n'.format(count)
    yield 'data: {0}\n\n'.format(count)
    count += 1

@app.route('/my_event_source')
def sse_request():
  return Response(
          event_stream(),
          mimetype='text/event-stream')

@app.route('/')
def page():
  return render_template('index.html')


if __name__ == '__main__':

  print "Please open a web browser to http://127.0.0.1:5000."

  # Spin up the app
  http_server = HTTPServer(WSGIContainer(app))
  http_server.listen(5000)
  IOLoop.instance().start()
在我的templates文件夹中,我有一个简单的index.html页面:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8" />
  <title>Test</title>

  <script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
  <script src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
  <script type="text/javascript" src="../static/sse_client.js"></script>

</head>
<body>
  <h3>Test</h3>

  <ul id="output">
  </ul>

</body>

</html>
该应用程序显示索引页面,但数据不会流式传输到该页面。我不知道我做错了什么。我想我需要另一双眼睛。我敢肯定这是一件非常次要和愚蠢的事情。

要使用url
“./static/sse_client.js”
您需要您的Web服务器或Flask应用程序来提供静态JavaScript文件。从烧瓶文档:

要为静态文件生成URL,请使用特殊的“静态”端点 姓名:

url\u用于('static',filename='style.css')

该文件必须作为static/style.css存储在文件系统中


Tornado的WSGIContainer不支持来自wsgi应用程序的流式响应。您可以将Flask与多线程或基于greenlet的wsgi服务器一起使用,也可以使用Tornado的本机RequestHandler接口,但当您将Flask和Tornado与WSGIContainer结合使用时则不行


结合烧瓶和龙卷风通常不是一个好主意;我想这不是问题所在。
var queue = [];
var interval = setInterval(function(){addItem()}, 1000);

function addItem(){
  if(queue.length > 0){
    var item = queue[0];
    queue.shift();
    $('#output').append(item);
  }
}

$(document).ready(

  function() {

    var sse = new EventSource('/my_event_source');
    console.log('blah');

    sse.onmessage = function(event) {

      console.log('A message has arrived!');
      var list_item = '<li>' + event.data + '</li>';
      console.log(list_item);
      queue.push(list_item);
    };
})
sse/
  sse_server.py
  static/
    sse_client.js
  templates/
    index.html