Created
June 24, 2010 03:54
-
-
Save ssadler/450954 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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): | |
""" | |
This will only be called in the main thread and should return immediately. | |
""" | |
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
I am trying to use this code and when I try to add the file descriptor to event loop I am getting an error
OSError: [WinError 10038] An operation was attempted on something that is not a socket
and I am trying this on Windows. So is the error specific to Windows? Does Windows handle sockets different than Linux?My code is like this:
Is it the correct way of adding a file descriptor?