def _request_handler(self, ch, meth, prop, body): _key = meth.routing_key try: _req = RpcRequestMessage.loads(body) _fn = self._get_function(_key, _req.version) self._logger.debug("Dipsatching Request: {}".format(_req)) if _req.args: _data = _fn(**_req.args) else: _data = _fn() _resp = RpcResponseMessage(0, return_value=_data) self._logger.debug("Preparing Response: {}".format(_resp)) _ret = _resp.dumps() except exp.ExceptionBase as ex: self._logger.info("Exception Detected: {}".format(ex)) _ret = RpcResponseMessage(ex.code, error_message=ex.message).dumps() except Exception as ex: self._logger.warning("Unhandled Exception Detected: {}".format(ex)) _ex = exp.UnhandledException(ex.message) _ret = RpcResponseMessage( _ex.code, error_message=_ex.message ).dumps() self._logger.debug("Dispatching Response: {}".format(_ret)) ch.basic_ack(delivery_tag=meth.delivery_tag) if prop.reply_to: self._logger.debug("Responding to request: {}".format(_key)) ch.basic_publish( exchange='', routing_key=prop.reply_to, body=str(_ret), properties=pika.BasicProperties( correlation_id=prop.correlation_id ) )
def get_response(self, correlation_id, clear_response=True, wait=False): while wait and not self.has_response(correlation_id): pass _resp = RpcResponseMessage.loads(self._response[correlation_id]) if clear_response: del self._response[correlation_id] return _resp