Skip to content

Commit

Permalink
Merge pull request #20 from tiagocoutinho/main
Browse files Browse the repository at this point in the history
Use `epoll` for `Bus.wait()` when available
  • Loading branch information
webknjaz authored Apr 26, 2024
2 parents 5e71299 + d1f5c44 commit 3f0fc1e
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions magicbus/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
import select
except ImportError:
select = None
else:
try:
epoll = select.epoll
except AttributeError:
epoll = None
import sys
import time
import traceback as _traceback
Expand Down Expand Up @@ -333,14 +338,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
Expand All @@ -352,6 +363,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)
Expand Down

0 comments on commit 3f0fc1e

Please sign in to comment.