From ab1646784428bf039c5400f34a7183d27938dba4 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Mon, 28 Dec 2020 17:16:28 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=92=A5=20Change=20command=20behaviour=20a?= =?UTF-8?q?nd=20sentry=20type=20annotations?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- royalnet/engineer/command.py | 9 +++++++-- royalnet/engineer/sentry.py | 14 +++++++------- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/royalnet/engineer/command.py b/royalnet/engineer/command.py index 59aa0ac8..f1e5332d 100644 --- a/royalnet/engineer/command.py +++ b/royalnet/engineer/command.py @@ -7,6 +7,7 @@ import royalnet.royaltyping as t import logging import re +import asyncio from . import teleporter from . import bullet @@ -95,8 +96,12 @@ class Command: """ A conversation which runs the command. """ - log.debug(f"Waiting for a message...") - msg = await (_sentry | wrench.Type(bullet.Message)) + log.debug(f"Getting a message from the queue...") + try: + msg: bullet.Message = (_sentry | wrench.Type(bullet.Message)).get_nowait() + except asyncio.QueueEmpty: + log.debug(f"No message found in the queue, returning...") + return log.debug(f"Getting text of {msg}...") text = await msg.text() diff --git a/royalnet/engineer/sentry.py b/royalnet/engineer/sentry.py index 2ae2bf05..b95ecbae 100644 --- a/royalnet/engineer/sentry.py +++ b/royalnet/engineer/sentry.py @@ -30,7 +30,7 @@ class Sentry(metaclass=abc.ABCMeta): raise NotImplementedError() @abc.abstractmethod - def get_nowait(self) -> bullet.Bullet: + def get_nowait(self): """ Try to get a single :class:`~.bullet.Bullet` from the pipeline, without blocking or handling discards. @@ -42,7 +42,7 @@ class Sentry(metaclass=abc.ABCMeta): raise NotImplementedError() @abc.abstractmethod - async def get(self) -> bullet.Bullet: + async def get(self): """ Try to get a single :class:`~.bullet.Bullet` from the pipeline, blocking until something is available, but without handling discards. @@ -53,7 +53,7 @@ class Sentry(metaclass=abc.ABCMeta): """ raise NotImplementedError() - async def wait(self) -> bullet.Bullet: + async def wait(self): """ Try to get a single :class:`~.bullet.Bullet` from the pipeline, blocking until something is available and is not discarded. @@ -143,10 +143,10 @@ class SentryFilter(Sentry): def __len__(self) -> int: return len(self.previous) + 1 - def get_nowait(self) -> bullet.Bullet: + def get_nowait(self): return self.previous.get_nowait() - async def get(self) -> bullet.Bullet: + async def get(self): return await self.previous.get() async def put(self, item) -> None: @@ -168,10 +168,10 @@ class SentrySource(Sentry): def __len__(self) -> int: return 1 - def get_nowait(self) -> bullet.Bullet: + def get_nowait(self): return self.queue.get_nowait() - async def get(self) -> bullet.Bullet: + async def get(self): return await self.queue.get() async def put(self, item) -> None: