Danila Migalin Danila Migalin - 1 year ago 124
Python Question

Stream result of blocking function to client in python Tornado HTTP server

I have various functions that are using blocking I/O to get some large amounts of data and can write it so stream/file-like object (with chunking etc); also I have tornado HTTP server to provide this data to clients. As far as I cannot store all data in memory, I should stream it to clients as I'm receiving it from its source. So i wrote something like that:

import logging


from concurrent.futures import ThreadPoolExecutor
from tornado import gen, httpserver, httpclient, web, ioloop, httputil, escape, locks, iostream
from threading import Event

def get_data(stream):
with open('/tmp/qq.dat') as file:
for chunk in iter(lambda: file.read(64*1024), b''):

class ProxyStream(object):
def __init__(self, request):
self._request = request

def write(self, data):
event = Event()
self._request.flush(callback=lambda: event.set())
return len(data)

class Test(web.RequestHandler):
def initialize(self, workers):
self._workers = workers

def get(self):
stream = ProxyStream(self)
yield self._workers.submit(get_data, stream)
logging.debug("GET done")

if __name__ == '__main__':
workers = ThreadPoolExecutor(4)
app = web.Application([
(r"/test", Test, {'workers': workers}),

server = httpserver.HTTPServer(app) server.bind(1488)

It code above, get_data() function reads some file (that might be very big) and writes it in chunks to stream passed as argument. Stream is emulated by ProxyStream object that writes received data to RequestHandler object waiting until chunk flushes to network.

It appears that this code works as expected, but I still has some doubts whether there are some pitfalls in this approach or maybe there's a better way to solve this problem?

Answer Source

Actually I faced some problems that lead me to solution that looks better for me.

RequestHandler write() and flush() methods are not thread-safe and should be called from thread where ioloop is running (see this and this). So correct way is to wrap write() & flush() into IOLoop.add_callback so they will be called on next ioloop iteration. Resulting code is smth like this:

class ProxyStream(object):
def __init__(self, handler, headers=[]):
    self._handler = handler

def sync_write(self, data, event):
    self._handler.flush(callback=lambda: event.set())

def write(self, data):
    if not self._handler.dead:
        event = Event()
        IOLoop.current().add_callback(self.sync_write, data, event)
        return len(data)
        raise IOError("Client has closed connection")

(RequestHandler passing itself to some synchronous code should set self.dead = True at on_connection_close() to stop streaming once client is disconnected)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download