def __call__(self, method, *args, **kargs): timeout = kargs.get('timeout', self._timeout) channel = self._multiplexer.channel() hbchan = HeartBeatOnChannel(channel, freq=self._heartbeat_freq, passive=self._passive_heartbeat) bufchan = BufferedChannel(hbchan, inqueue_size=kargs.get('slots', 100)) request_event = self._generate_request_event(bufchan, method, args) bufchan.emit_event(request_event) try: if kargs.get('async', False) is False: return self._process_response(request_event, bufchan, timeout) async_result = gevent.event.AsyncResult() gevent.spawn(self._process_response, request_event, bufchan, timeout).link(async_result) return async_result except: # XXX: This is going to be closed twice if async is false and # _process_response raises an exception. I wonder if the above # async branch can raise an exception too, if no we can just remove # this code. bufchan.close() raise
def __call__(self, method, *args, **kargs): # We modify zerorpc's __call__ method to remove the heartbeat feature since it may cause server-side requests # to get interrupted during processing if it detects a lost remote and it may also fail if the client and # server don't have the same heartbeat frequency set. # # The following code is taken from zerorpc/core.py: timeout = kargs.get('timeout', self._timeout) channel = self._multiplexer.channel() bufchan = BufferedChannel(channel, inqueue_size=kargs.get('slots', 100)) xheader = self._context.hook_get_task_context() if hasattr(bufchan, 'new_event'): request_event = bufchan.new_event(method, args, xheader) else: request_event = bufchan.create_event(method, args, xheader) self._context.hook_client_before_request(request_event) bufchan.emit_event(request_event) try: if kargs.get('async', False) is False: return self._process_response(request_event, bufchan, timeout) async_result = gevent.event.AsyncResult() gevent.spawn(self._process_response, request_event, bufchan, timeout).link(async_result) return async_result except: # XXX: This is going to be closed twice if async is false and # _process_response raises an exception. I wonder if the above # async branch can raise an exception too, if no we can just remove # this code. bufchan.close() raise
def _async_task(self, initial_event): ### TODO: Use ZeroRPC middleware functionality protocol_v1 = initial_event.header.get('v', 1) < 2 channel = self._multiplexer.channel(initial_event) hbchan = HeartBeatOnChannel(channel, freq=self._heartbeat_freq, passive=protocol_v1) bufchan = BufferedChannel(hbchan) event = bufchan.recv() try: self._context.middleware_load_task_context(event.header) # TODO: support non Req/Rep patterns, such as pubsub, pushpull call = Call(event.name, event.args) # TODO: Adam: add ``peer=...`` here result = self.execute_call(call) bufchan.emit('OK', (result,), self._context.middleware_get_task_context()) except LostRemote: self._print_traceback(protocol_v1) except Exception: exception_info = self._print_traceback(protocol_v1) bufchan.emit('ERR', exception_info, self._context.middleware_get_task_context()) finally: bufchan.close()