commit 933a7b30663c2ab41115c2796a58ea236633ead5 Author: Illia Volochii illia.volochii@gmail.com Date: Fri Apr 17 23:03:12 2020 +0300
Add support for asynchronous event handlers --- stem/control.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-)
diff --git a/stem/control.py b/stem/control.py index 86af0e8a..20d7c8ef 100644 --- a/stem/control.py +++ b/stem/control.py @@ -844,7 +844,7 @@ class BaseController(object): await def __aexit__(self, exit_type: Optional[Type[BaseException]], value: Optional[BaseException], traceback: Optional[TracebackType]) -> None: await self.close()
- def _handle_event(self, event_message: stem.response.ControlMessage) -> None: + async def _handle_event(self, event_message: stem.response.ControlMessage) -> None: """ Callback to be overwritten by subclasses for event listening. This is notified whenever we receive an event from the control socket. @@ -975,7 +975,7 @@ class BaseController(object): while True: try: event_message = self._event_queue.get_nowait() - self._handle_event(event_message) + await self._handle_event(event_message) self._event_queue.task_done()
# Attempt to finish processing enqueued events when our controller closes @@ -3802,7 +3802,7 @@ class Controller(BaseController): else: log.warn('We were unable assert ownership of tor through TAKEOWNERSHIP, despite being configured to be the owning process through __OwningControllerProcess. (%s)' % response)
- def _handle_event(self, event_message: stem.response.ControlMessage) -> None: + async def _handle_event(self, event_message: stem.response.ControlMessage) -> None: event = None # type: Optional[stem.response.events.Event]
try: @@ -3822,7 +3822,10 @@ class Controller(BaseController): if listener_type == event_type: for listener in event_listeners: try: - listener(event) + if asyncio.iscoroutinefunction(listener): + await listener(event_message) + else: + listener(event_message) except Exception as exc: log.warn('Event listener raised an uncaught exception (%s): %s' % (exc, event))
tor-commits@lists.torproject.org