Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Concurrently streaming response using Falcon and Gevent
# Fire this using gunicorn falcon_gevent_streaming
# and head to in a browser.
# Requirements:
# pip install falcon gevent
import falcon
import gevent
from gevent.queue import Queue
from gevent.event import Event
application = falcon.API()
def do_work(q, start=0, end=12, wait=0, wait_each=0, color='black'):
if wait:
for i in xrange(start, end):
if wait_each:
q.put('<span style="color: {};">{}</span> '.format(color, i))
def finish_when_ready(ev, q):
def finish_after(ev, after):
class Thing:
def on_get(self, req, resp):
q = Queue()
e = Event()
q.put(' ' * 1024)
gevent.spawn(do_work, q, start=2, end=10, wait=3, color='red')
gevent.spawn(do_work, q, wait_each=1, color='green')
gevent.spawn(do_work, q, wait=5, wait_each=0.25, color='blue')
gevent.spawn(finish_when_ready, e, q)
gevent.spawn(finish_after, e, 15)
resp.content_type = 'text/html' = q
things = Thing()
application.add_route('/', things)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment