def _call_nowait(self, hints, name, args, kwargs, topics=(), raw=False): header = self._make_header(name, b'', NO_REPLY, hints) payload = self._pack(args, kwargs, raw) try: safe(send, self.socket, header, payload, topics, zmq.NOBLOCK) except zmq.Again: pass # ignore.
def _call_nowait(self, hints, name, args, kwargs, topics=(), raw=False): header = [name.encode(ENCODING), '', NO_REPLY] header.extend(hints) payload = self._pack(args, kwargs, raw) try: safe(send, self.socket, header, payload, topics, zmq.NOBLOCK) except zmq.Again: pass # ignore.
def send_raw(self, socket, method, payload, call_id, task_id, topics=()): if not socket: return # normal tuple is faster than namedtuple. header = [int2byte(method), call_id, task_id] try: safe(send, socket, header, payload, topics, zmq.NOBLOCK) except (zmq.Again, zmq.ZMQError): pass # ignore.
def send_reply(self, socket, method, value, call_id, task_id, topics=()): if not socket: return # normal tuple is faster than namedtuple. header = [chr(method), call_id, task_id] payload = self.pack(value) try: safe(send, socket, header, payload, topics, zmq.NOBLOCK) except (zmq.Again, zmq.ZMQError): pass # ignore.
def __call__(self): """Runs the worker. While running, an RPC service is online.""" poller = zmq.Poller() for socket in self.sockets: poller.register(socket, zmq.POLLIN) group = self.greenlet_group msgs = [] capture = msgs.extend def accept(socket, call, args, kwargs, topics): group.spawn(self.work, socket, call, args, kwargs, topics) group.join(0) def reject(socket, call, topics): __, call_id, reply_to, __, __ = call reply_socket, topics = self.replier(socket, topics, reply_to) self.reject(reply_socket, call_id, topics) def reject_if(call, topics): if self.reject_if(call, topics): return True try: __, rpc_spec = self.find_call_target(call) except KeyError: return True return rpc_spec.reject_if.__get__(self.app)(call, topics) try: while True: for socket, event in safe(poller.poll): assert event & zmq.POLLIN del msgs[:] try: header, payload, topics = recv(socket, capture=capture) call = parse_call(header) if group.full() or reject_if(call, topics): reject(socket, call, topics) continue args, kwargs = self.unpack(payload) except: # If any exception occurs in the above block, # the messages are treated as malformed. handle = self.malformed_message_handler if handle is not None: exc_info = sys.exc_info() handle(self, exc_info, msgs[:]) del handle continue # Accept the call. accept(socket, call, args, kwargs, topics) # Release memory. try: del header, payload, topics del call del args, kwargs except UnboundLocalError: # Stop at the first error. pass finally: group.kill()
def send_call(): try: safe(send, self.socket, header, payload, topics, zmq.NOBLOCK) except zmq.Again: raise Undelivered('emission was not delivered')