def server_main(loop, path): """Run in the client after the fork.""" loop.fork() logger.debug('forked function') sigintwatcher = pyev.Signal( signal.SIGINT, loop, lambda watcher, events: logger.info('interrupt ignored')) sigintwatcher.start() sigtermwatcher = pyev.Signal(signal.SIGTERM, loop, server_stop) sigtermwatcher.start() adder = AdderService() dispatcher = ObjectDispatch(adder) pickle_factory = PickleProtocolFactory(dispatcher) pickle_server = UnixServer(loop, pickle_factory, path) pickle_server.start() msgpack_factory = MsgPackProtocolFactory(dispatcher) msgpack_server = UnixServer(loop, msgpack_factory, path + '_mp') msgpack_server.start() logger.debug('running server loop') import cProfile cProfile.runctx('loop.loop()', None, {'loop': loop}, 'server_profile') logger.debug('server unlooped')
def main(): path = 'adder_socket' loop = pyev.default_loop() sigwatcher = pyev.Signal( signal.SIGINT, loop, lambda watcher, events: watcher.loop.unloop(pyev.EVUNLOOP_ALL)) sigwatcher.start() p = Process(loop, server_main, loop, 'adder_socket') p.start() pickle_factory = PickleProtocolFactory() pickle_client = UnixClient(loop, pickle_factory, path) retries = 10 while retries: try: pickle_client.connect().result() retries = 0 except Exception as e: time.sleep(0.1) retries -= 1 proxy = pickle_factory.proxy(0).result() start = time.time() s = 0 for i in range(10000): s = proxy.call('add', 1, s) stop = time.time() logger.info( 'pickle-rpc took {} seconds to perform {} calls, {} calls per second', stop - start, s, s / (stop - start)) start = time.time() for i in range(10000): proxy.notify('add', 1, s) proxy.call('add', 1, s) stop = time.time() logger.info( 'pickle-rpc took {} seconds to perform {} notifications, {} notifies per second', stop - start, 10000, 10000 / (stop - start)) msgpack_factory = MsgPackProtocolFactory() msgpack_client = UnixClient(loop, msgpack_factory, path + '_mp') retries = 10 while retries: try: msgpack_client.connect().result() retries = 0 except Exception as e: time.sleep(0.1) retries -= 1 proxy = msgpack_factory.proxy(0).result() start = time.time() s = 0 for i in range(10000): s = proxy.call('add', 1, s) stop = time.time() logger.info( 'msgpack-rpc took {} seconds to perform {} calls, {} calls per second', stop - start, s, s / (stop - start)) start = time.time() for i in range(10000): proxy.notify('add', 1, s) proxy.call('add', 1, s) stop = time.time() logger.info( 'msgpack-rpc took {} seconds to perform {} notifications, {} notifies per second', stop - start, 10000, 10000 / (stop - start)) p.stop()
def connect(self): self.factory = MsgPackProtocolFactory() self.client = UnixClient(self.loop, self.factory, self.path) d = self.client.connect() d.add_callback(self.connected) return d
def listen_init(self): """Setup the service to listen for clients.""" self.dispatcher = ObjectDispatch(self) self.factory = MsgPackProtocolFactory(self.dispatcher) self.server = UnixServer(self.loop, self.factory, self.path) self.server.start()