async def get_message(): wait_coro = asyncio.ensure_future(channel.wait_message()) timeout_coro = asyncio.ensure_future( asyncio.sleep(self.request_timeout)) done = set() try: done, running = await asyncio.wait( [wait_coro, timeout_coro], return_when=asyncio.FIRST_COMPLETED) except Exception as error: # pragma: no cover logger.error('Error while waiting for message:', error) raise exceptions.RadicalException( f'Error while waiting for message: {str(error)}') else: if timeout_coro in done: raise exceptions.TimeoutException( 'Timeout while waiting for response.') message = await channel.get() return message finally: timeout_coro.cancel() try: await self.pool.unsubscribe(channel_name) except Exception as error: # pragma: no cover raise exceptions.TransportException(str(error)) channel.close()
async def get_next_request(self): source_name = QUEUE_PREFIX + self.queue_name try: result = await self.pool.execute('blpop', source_name, 1) if result is not None: logger.debug('BLPOP %s', source_name) return result[1] except Exception as error: logger.error(f'ERROR: {repr(error)}, retrying in 1 second') await asyncio.sleep(1)
async def _accept(self) -> RadicalRequest: request = await self.transport.get_next_request() if request is not None: try: radical_request = self.serializer.decode_request(request) except ProtocolError as error: logger.error(f'Deserialization failed: {error}') else: return radical_request return NoRequest()
async def _process_request( self, radical_request: RadicalRequest) -> Optional[RadicalResponse]: logger.info(f'Received request {radical_request}') result, error = None, None try: signature = radical_request.signature method = self.methods[signature.method] if asyncio.iscoroutinefunction(method): result = await method(*signature.args, **signature.kwargs) else: result = method(*signature.args, **signature.kwargs) except Exception as method_error: # TODO: Include traceback error = str(method_error) if error: logger.error(f'WARNING: Error occured: {error}') return RadicalResponse(request=radical_request, result=result, error=error)
def _exception_handler(self, loop, data) -> NoReturn: logger.error(f'Exception in loop {loop}: {data}')