"""Demonstration of using tornadose EventSource handlers to publish
the Fibonacci sequence to listeners.

"""

import signal
from tornado import gen
from tornado.ioloop import IOLoop
from tornado.options import define, options
from tornado.locks import Event
from tornado.web import Application, RequestHandler
from tornado.httpclient import AsyncHTTPClient
from tornadose.handlers import EventSource
from tornadose.stores import DataStore

define('port', default=9000)
define('profile', default=False)

html = """
<div id="messages"></div>
<script type="text/javascript">
  var source = new EventSource('/fibonacci');
  source.onmessage = function(message) {
    var div = document.getElementById("messages");
    div.innerHTML = message.data + "<br>" + div.innerHTML;
  };
</script>"""

store = DataStore()
finish = Event()
client = AsyncHTTPClient()


def fibonacci():
    a, b = 0, 1
    while True:
        yield a
        a, b = b, a + b


class MainHandler(RequestHandler):
    def get(self):
        self.write(html)


@gen.coroutine
def generate_sequence():
    for number in fibonacci():
        store.submit(number)
        yield gen.sleep(1)
        if finish.is_set():
            break


@gen.coroutine
def subscribe():
    """Subscribe to the stream with an AsyncHTTPClient."""
    def callback(data):
        print(data)

    if options.profile:
        yield client.fetch(
            'http://127.0.0.1:{}/fibonacci'.format(options.port),
            streaming_callback=callback)


@gen.coroutine
def main():
    app = Application(
        [
            ('/', MainHandler),
            ('/fibonacci', EventSource, {'store': store})
        ],
        debug=True
    )
    app.listen(options.port)
    yield [generate_sequence(), subscribe()]


def shutdown(sig, frame):
    finish.set()
    client.close()
    IOLoop.instance().stop()

if __name__ == "__main__":
    options.parse_command_line()
    signal.signal(signal.SIGINT, shutdown)

    if options.profile:
        IOLoop.instance().call_later(10, lambda: shutdown(None, None))
    IOLoop.instance().run_sync(main)

@@ -308,55 +308,8 @@
 ler%0A
-from tornado.httpclient import AsyncHTTPClient%0A
 from
@@ -347,16 +347,16 @@
 tSource%0A
+
 from tor
@@ -419,41 +419,8 @@
 000)
-%0Adefine('profile', default=False)
 %0A%0Aht
@@ -743,35 +743,8 @@
 t()%0A
-client = AsyncHTTPClient()%0A
 %0A%0Ade
@@ -1092,305 +1092,8 @@
 k%0A%0A%0A
-@gen.coroutine%0Adef subscribe():%0A    %22%22%22Subscribe to the stream with an AsyncHTTPClient.%22%22%22%0A    def callback(data):%0A        print(data)%0A%0A    if options.profile:%0A        yield client.fetch(%0A            'http://127.0.0.1:%7B%7D/fibonacci'.format(options.port),%0A            streaming_callback=callback)%0A%0A%0A
 @gen
@@ -1313,17 +1313,16 @@
   yield 
-%5B
 generate
@@ -1336,22 +1336,8 @@
 ce()
-, subscribe()%5D
 %0A%0A%0Ad
@@ -1382,27 +1382,8 @@
 t()%0A
-    client.close()%0A
     
@@ -1468,16 +1468,16 @@
 _line()%0A
+
     sign
@@ -1514,104 +1514,8 @@
 own)
-%0A%0A    if options.profile:%0A        IOLoop.instance().call_later(10, lambda: shutdown(None, None))
 %0A   

Remove profiling in demo