def __init__(self): """ 初始化,启动调度系统和 rpc 服务 """ self._scheduler_srv = SchedulerServer() self._server = Server(self._scheduler_srv) self._server.bind(self.endpoint)
def run_rpc_server(multivisor, bind, future_server): multivisor._log.info("0RPC: spawn server on {}...".format(os.getpid())) watcher = hub.get_hub().loop.async_() stop_event = threading.Event() watcher.start(lambda: spawn(multivisor._dispatch_event)) server = None try: context = Context() context.register_middleware(ServerMiddleware()) server = Server(multivisor, context=context) server._stop_event = stop_event server.bind(bind) future_server.put((server, watcher)) multivisor._log.info("0RPC: server running!") server.run() multivisor._log.info("0RPC: server stopped!") except Exception as err: future_server.put(err) finally: watcher.stop() del server # prevent reusage of this loop because supervisor closes all ports # when a restart happens. It actually doesn't help preventing a crash hub.get_hub().destroy(destroy_loop=True) multivisor._log.info("0RPC: server thread destroyed!") stop_event.set()
class RPCServer: """ 事件 rpc 服务端 """ endpoint = f'tcp://{settings.RPC_BIND}:{settings.RPC_PORT}' def __init__(self): """ 初始化,启动调度系统和 rpc 服务 """ self._scheduler_srv = SchedulerServer() self._server = Server(self._scheduler_srv) self._server.bind(self.endpoint) def run(self): """ 启动 rpc 服务 """ self._server.run()
def run(xml_rpc, bind=DEFAULT_BIND): channel = Queue() supervisor = Supervisor(xml_rpc) t1 = spawn(event_consumer_loop, channel, supervisor.publish_event) t2 = spawn(event_producer_loop, channel.put) server = Server(supervisor) server.bind(bind) server.run()
def run_rpc_server(multivisor, bind, future_server): multivisor._log.info('0RPC: spawn server on {}...'.format(os.getpid())) watcher = hub.get_hub().loop.async() watcher.start(lambda: spawn(multivisor._dispatch_event)) server = Server(multivisor) server.bind(bind) future_server.put((server, watcher)) multivisor._log.info('0RPC: server running!') server.run()
def run(bind=DEFAULT_BIND): def event_consumer_loop(): # xmlrpclib is not reentrant. We might have several greenlets accessing # supervisor at the same time so we serialize event treatment here lock = RLock() for event in channel: try: with lock: supervisor.publish_event(event) except: logging.exception('Error processing %s', event) channel = Queue() supervisor = Supervisor(channel) spawn(event_consumer_loop) spawn(event_dispatcher_loop, channel.put) server = Server(supervisor) server.bind(bind) server.run()
from .mqtt_server import MQTTServer from zerorpc import Server if __name__ == '__main__': server = Server(MQTTServer) server.bind('tcp://0.0.0.0:2048') server.run()