Tornado has a great, simple interface to epoll, and to capitalize on
that for other servers (not just HTTP), I'd like to see an abstracted
interface to reduce boilerplate. I quickly created BaseServer and
BaseConnectionHandler classes[1] that showcase how they might be
abstracted. It's possible to use these classes under the httpserver
classes. This is a proof-of-concept, most of the code ripped from
httpserver; I'm hoping to get some feedback.
Here's an example script of an Echo server using these classes, which
is considerably smaller than the original implementation I wrote [2]:
from tornado.baseserver import BaseServer, BaseConnectionHandler
from tornado import ioloop
class EchoConnectionHandler(BaseConnectionHandler):
def __init__(self, stream, address, server):
super(EchoConnectionHandler, self).__init__(stream,
address, server)
self.read()
def read(self):
self.stream.read_until("\r\n", self.eof_callback)
def eof_callback(self, data):
self.stream.write(data)
self.read()
server = BaseServer(EchoConnectionHandler)
server.listen(8000)
ioloop.IOLoop.instance().start()
[1]:
http://github.com/JaredKuolt/tornado/blob/master/tornado/baseserver.py
[2]:
http://superjared.com/entry/building-echo-server-using-tornado/