Skip to content

Instantly share code, notes, and snippets.

@ssadler
Created June 24, 2010 03:53
Show Gist options
  • Save ssadler/450953 to your computer and use it in GitHub Desktop.
Save ssadler/450953 to your computer and use it in GitHub Desktop.
import tornado.web
from multiprocessing import Pipe
from _multiprocessing import Connection
class MultiprocessApplication(tornado.web.Application):
def __init__(self, *args, **kwargs):
self._conn_main_recv, self._conn_main_send = Pipe()
tornado.web.Application.__init__(self, *args, **kwargs)
def setup(self, ioloop, is_main_process):
self.is_main_process = is_main_process
self._conn_child_recv, self._conn_child_send = Pipe()
if is_main_process:
ioloop.add_handler(self._conn_main_recv.fileno(),
self._handle_child_event,
ioloop.READ)
def communicate(self, data):
to_send = (self._conn_child_send.fileno(), data)
self._conn_main_send.send(to_send)
return self._conn_child_recv.recv()
def _handle_child_event(self, fd, events):
conn_fileno, data = self._conn_main_recv.recv()
print 'got data:', data
Connection(conn_fileno).send('acknowledged')
############## To start the server:
http_server = tornado.httpserver.HTTPServer(app)
http_server.bind(port)
is_main = http_server.start(2)
ioloop = tornado.ioloop.IOLoop.instance()
if hasattr(app, 'setup'):
app.setup(ioloop, is_main)
ioloop.start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment