def sync_call(self, apiname, args={}, timeout=5): """ 给定参数args,同步调用RPCServer的apiname服务, 返回该服务的处理结果。如果超时,返回None。 Args: apiname (str): 服务API名称。 args (dict): 给服务API的参数。 handler (function): 回调函数。 """ log.debug('RPCClient [%s] sync_call: %s' % (self._name, apiname)) if not isinstance(args, dict): self._timeout = 0 self._sync_ret = None raise InvalidRPCClientArguments(argtype=type(args)) self.rid += 1 args['apiname'] = apiname args['rid'] = self.rid with self._sync_call_time_lock: self._sync_call_time = datetime.now() self._timeout = timeout with self._handlers_lock: self._handlers[self.rid] = None self._event_engine.emit(Event(self.EVENT_FROM_CLIENT, args)) self._waiting_server_data() ret = self._sync_ret return ret
def _process(self, event): """ 基于多线程的处理事件 """ if event.route not in self._routes: log.warning("事件%s 没有被处理" % event.route) return for handler in self._routes[event.route]: try: log.debug("处理事件%s" % event.route) _thread.start_new_thread(handler, (event, )) #handler(event) except Exception as e: log.error(e)
def _run_timer(self): # @TODO 用python自带的Event替代。 while True: if not self._timeout == 0: with self._sync_call_time_lock: mtime = self._sync_call_time delta = (datetime.now() - mtime).seconds if delta >= self._timeout: # print("timeout", self._timeout, delta) # 不可重入,保证self.rid就是超时的那个 with self._handlers_lock: del self._handlers[self.rid] log.debug("[RPCClient._runtimer] 处理超时, delete rid; %s" % self.rid) self._timeout = 0 self._notify_server_data() time.sleep(self._timer_sleep)
def call(self, apiname, args, handler): """ 给定参数args,异步调用RPCServer的apiname服务, 返回结果做为回调函数handler的参数。 Args: apiname (str): 服务API名称。 args (dict): 给服务API的参数。 handler (function): 回调函数。 """ if not isinstance(args, dict): raise InvalidRPCClientArguments(argtype=type(args)) assert (handler is not None) log.debug('RPCClient [%s] sync_call: %s' % (self._name, apiname)) self.rid += 1 args['apiname'] = apiname args['rid'] = self.rid self._event_engine.emit(Event(self.EVENT_FROM_CLIENT, args)) with self._handlers_lock: self._handlers[self.rid] = handler
def _process_request(self, event): args = event.args rid = args['rid'] apiname = args['apiname'] del args['rid'] del args['apiname'] log.debug('RPCServer [%s] process: %s' % (self._name, apiname)) try: with self._routes_lock: handler = self._routes[apiname] # @TODO async ret = handler(**args) except Exception as e: log.exception(e) else: args = {'ret': ret, 'rid': rid} log.debug('RPCServer [%s] emit %s' % (self._name, str(self.EVENT_FROM_SERVER))) self._event_engine.emit(Event(self.EVENT_FROM_SERVER, args))
def _process_apiback(self, event): assert (event.route == self.EVENT_FROM_SERVER) self._timeout = 0 rid = event.args['rid'] try: with self._handlers_lock: handler = self._handlers[rid] except KeyError: log.info('[RPCClient._process_apiback] 放弃超时任务的返回结果') else: try: if handler: # 异步 handler(event.args['ret']) else: # 同步 self._sync_ret = event.args['ret'] self._notify_server_data() except Exception as e: log.error(e) log.debug("[RPCClient._process_apiback] 删除已经完成的任务 rid; %s" % rid) with self._handlers_lock: del self._handlers[rid]
def _run_server(self): strevent = self._server_recv_event_socket.recv() self._broadcast_event_socket.send(strevent) log.debug("[engine: %s] broadcast message: %s" % (self._name, strevent))
def _run_client(self): message = self._client_recv_event_socket.recv() event = Event.message_to_event(message) log.debug("[engine: %s] receive message: %s" % (self._name, event)) self._queue_engine.emit(event)