Skip to content

Instantly share code, notes, and snippets.

@ssadler
Created June 24, 2010 18:24
Show Gist options
  • Save ssadler/451767 to your computer and use it in GitHub Desktop.
Save ssadler/451767 to your computer and use it in GitHub Desktop.
import os
import tornado.httpserver
import tornado.ioloop
import tornado.web
from multiprocessing import Pipe
class HelloHandler(tornado.web.RequestHandler):
def get(self):
self.write(self.application.communicate('hello world'))
class MultiprocessApplication(tornado.web.Application):
def __init__(self, *args, **kwargs):
tornado.web.Application.__init__(self, *args, **kwargs)
def fork(self, fork_num):
if fork_num == 0:
self._pipes = {0: Pipe()}
conns = Pipe()
pid = os.fork()
if pid == 0:
self._pid = os.getpid()
self._pipes[pid or self._pid] = conns
return pid
def setup(self, ioloop, is_main_process):
self.is_main_process = is_main_process
if is_main_process:
ioloop.add_handler(self._pipes[0][0].fileno(),
self._handle_child_event,
ioloop.READ)
def communicate(self, data):
to_send = (self._pid, data)
self._pipes[0][1].send(to_send)
return self._pipes[self._pid][0].recv()
def _handle_child_event(self, fd, events):
child_pid, data = self._pipes[0][0].recv()
data = "pid %s says: %s" % (child_pid, data)
self._pipes[child_pid][1].send(data)
app = MultiprocessApplication([
(r"/", HelloHandler),
])
http_server = tornado.httpserver.HTTPServer(app)
http_server.bind(8889)
is_main = http_server.start(2, wait=False, fork_callback=app.fork)
ioloop = tornado.ioloop.IOLoop.instance()
app.setup(ioloop, is_main)
ioloop.start()
# Patch for tornado/httpserver.py
#
#diff --git a/tornado/httpserver.py b/tornado/httpserver.py
#index 2f0fa04..1568d60 100644
#--- a/tornado/httpserver.py
#+++ b/tornado/httpserver.py
#@@ -150,7 +150,7 @@ class HTTPServer(object):
# self._socket.bind((address, port))
# self._socket.listen(128)
#
#- def start(self, num_processes=None):
#+ def start(self, num_processes=None, wait=True, fork_callback=None):
# """Starts this server in the IOLoop.
#
# By default, we detect the number of cores available on this machine
#@@ -181,19 +181,22 @@ class HTTPServer(object):
# if num_processes > 1:
# logging.info("Pre-forking %d server processes", num_processes)
# for i in range(num_processes):
#- if os.fork() == 0:
#+ pid = fork_callback(i) if fork_callback else os.fork()
#+ if pid == 0:
# self.io_loop = ioloop.IOLoop.instance()
# self.io_loop.add_handler(
# self._socket.fileno(), self._handle_events,
# ioloop.IOLoop.READ)
#- return
#- os.waitpid(-1, 0)
#+ return False
#+ if wait:
#+ os.waitpid(-1, 0)
# else:
# if not self.io_loop:
# self.io_loop = ioloop.IOLoop.instance()
# self.io_loop.add_handler(self._socket.fileno(),
# self._handle_events,
# ioloop.IOLoop.READ)
#+ return True
#
# def stop(self):
# self.io_loop.remove_handler(self._socket.fileno())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment