Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 17 additions & 9 deletions Lib/selectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,9 @@ class PollSelector(_PollLikeSelector):

if hasattr(select, 'epoll'):

_NOT_EPOLLIN = ~select.EPOLLIN
NOT_EPOLLOUT = ~select.EPOLLOUT

class EpollSelector(_PollLikeSelector):
"""Epoll-based selector."""
_selector_cls = select.epoll
Expand All @@ -452,23 +455,28 @@ def select(self, timeout=None):
# epoll_wait() expects `maxevents` to be greater than zero;
# we want to make sure that `select()` can be called when no
# FD is registered.
max_ev = max(len(self._fd_to_key), 1)
max_ev = len(self._fd_to_key) or 1

ready = []
try:
fd_event_list = self._selector.poll(timeout, max_ev)
except InterruptedError:
return ready
for fd, event in fd_event_list:
events = 0
if event & ~select.EPOLLIN:
events |= EVENT_WRITE
if event & ~select.EPOLLOUT:
events |= EVENT_READ

key = self._fd_to_key.get(fd)
fd_to_key = self._fd_to_key
for fd, event in fd_event_list:
key = fd_to_key.get(fd)
if key:
ready.append((key, events & key.events))
ready.append(
(
key,
(
(event & NOT_EPOLLIN and EVENT_WRITE)
| (event & NOT_EPOLLOUT and EVENT_READ)
)
& key.events
)
)
return ready

def close(self):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
:mod:`selectors`: Reduce EpollSelector overhead by moving code outside of the loop and avoiding a call to ``max()``.