def __init__(self, event_loop = ThreadedEventLoop()): Process.__init__(self) self.server_pipe, self.client_pipe = Pipe() self.server_router = Router(self.server_pipe) self.client_router = Router(self.client_pipe) self.srv_command_pipe = self.server_router.create() self.cli_command_pipe = self.client_router.create(self.srv_command_pipe.id) self.children = {} self.client_event_loop = event_loop self.client_event_loop.register_hook(self.client_router.eventloop_hook)
class InsulatedFactory(Process): def __init__(self, event_loop = ThreadedEventLoop()): Process.__init__(self) self.server_pipe, self.client_pipe = Pipe() self.server_router = Router(self.server_pipe) self.client_router = Router(self.client_pipe) self.srv_command_pipe = self.server_router.create() self.cli_command_pipe = self.client_router.create(self.srv_command_pipe.id) self.children = {} self.client_event_loop = event_loop self.client_event_loop.register_hook(self.client_router.eventloop_hook) def start_event_loop(self): self.client_event_loop.start() def stop_event_loop(self): self.client_event_loop.stop() def _process_children(self): for i in self.children.keys(): if self.children[i]['pipe'].poll(): response = self.children[i]['pipe'].recv() self.children[i]['parent_pipe'].send(response) logger.debug(msg("Child ", i, " sending response.")) if self.children[i]['parent_pipe'].poll(): req = self.children[i]['parent_pipe'].recv() self.children[i]['pipe'].send(req) logger.debug(msg("Child ", i, " receiving request.")) def run(self): logger.debug(msg("running.")) self.stop_event_loop() while True: self._process_children() if self.srv_command_pipe.poll(): req = self.srv_command_pipe.recv() logger.debug(msg("incoming data on command pipe ", req)) if req.message_name == 'create_object': try: obj_id = self._create_object(req.object_class, *req.args, **req.kwargs) response = rpc('created_object') response.object_id = obj_id except Exception, e: logger.warn(msg("Object could not be created, exception thrown: ", e)) response = rpc('created_object') response.object_id = None response.exception = e self.srv_command_pipe.send(response) elif req.message_name == 'terminate_object': self._server_terminate_object(req.object_id) elif req.message_name == 'terminate': for obj in self.children: self._server_terminate_object(obj) response = rpc('terminated') self.srv_command_pipe.send(response)