From f4618dbf6738b983dec261612fa4ffb7ed0ab3ee Mon Sep 17 00:00:00 2001 From: Jose Tiago Macara Coutinho Date: Thu, 14 Dec 2023 16:33:01 +0100 Subject: [PATCH 1/2] Use epoll for Bus.wait() when available --- magicbus/base.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/magicbus/base.py b/magicbus/base.py index 2a2a68f8..3cd54d15 100644 --- a/magicbus/base.py +++ b/magicbus/base.py @@ -15,6 +15,10 @@ import random try: import select + try: + epoll = select.epoll + except AttributeError: + epoll = None except ImportError: select = None import sys @@ -333,14 +337,20 @@ def wait(self, state, interval=0.1, channel=None, sleep=False): pipe = os.pipe() read_fd, write_fd = pipe self._state_transition_pipes.add(pipe) + if epoll: + poller = epoll(1) + poller.register(read_fd, select.EPOLLIN) def _wait(): try: while self.state not in _states_to_wait_for: if select: try: - r, w, x = select.select([read_fd], [], [], interval) - if r: + if epoll: + readers = poller.poll(interval) + else: + readers = select.select([read_fd], [], [], interval)[0] + if readers: os.read(read_fd, 1) except (select.error, OSError): # Interrupted due to a signal (being handled by some @@ -352,6 +362,8 @@ def _wait(): self.publish(channel) finally: self._state_transition_pipes.discard(pipe) + if epoll: + poller.close() # NOTE: Closing the write file descriptor first # NOTE: to prevent "Broken pipe" in `self._transition()`. os.close(write_fd) From d1f5c4468127bd63a4ccb5295b3cdc87faec1600 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sviatoslav=20Sydorenko=20=28=D0=A1=D0=B2=D1=8F=D1=82=D0=BE?= =?UTF-8?q?=D1=81=D0=BB=D0=B0=D0=B2=20=D0=A1=D0=B8=D0=B4=D0=BE=D1=80=D0=B5?= =?UTF-8?q?=D0=BD=D0=BA=D0=BE=29?= Date: Fri, 26 Apr 2024 20:20:49 +0200 Subject: [PATCH 2/2] Move the `epoll` detection to `else` block of `try` --- magicbus/base.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/magicbus/base.py b/magicbus/base.py index 3cd54d15..bc33917e 100644 --- a/magicbus/base.py +++ b/magicbus/base.py @@ -15,12 +15,13 @@ import random try: import select +except ImportError: + select = None +else: try: epoll = select.epoll except AttributeError: epoll = None -except ImportError: - select = None import sys import time import traceback as _traceback