Kaydet (Commit) 62c7d90b authored tarafından Giampaolo Rodola's avatar Giampaolo Rodola Kaydeden (comit) GitHub

#30014: refactor poll-related classes (#1035)

* #30014: refactor poll-related classes so that poll(), epoll() and devpoll() share the same methods for register(), unregister(), close() and select()

* remove unused attribute

* use specific class attributes instead of select.* constants

* have all classes except SelectSelector a _selector attribute

* BaseException -> Exception

* be explicit in defining a close() method only for selectors which have it

* fix AttributeError
üst 753bca39
...@@ -339,31 +339,41 @@ class SelectSelector(_BaseSelectorImpl): ...@@ -339,31 +339,41 @@ class SelectSelector(_BaseSelectorImpl):
return ready return ready
if hasattr(select, 'poll'): class _PollLikeSelector(_BaseSelectorImpl):
"""Base class shared between poll, epoll and devpoll selectors."""
class PollSelector(_BaseSelectorImpl): _selector_cls = None
"""Poll-based selector."""
def __init__(self): def __init__(self):
super().__init__() super().__init__()
self._poll = select.poll() self._selector = self._selector_cls()
def register(self, fileobj, events, data=None): def register(self, fileobj, events, data=None):
key = super().register(fileobj, events, data) key = super().register(fileobj, events, data)
poll_events = 0 poller_events = 0
if events & EVENT_READ: if events & EVENT_READ:
poll_events |= select.POLLIN poller_events |= self._EVENT_READ
if events & EVENT_WRITE: if events & EVENT_WRITE:
poll_events |= select.POLLOUT poller_events |= self._EVENT_WRITE
self._poll.register(key.fd, poll_events) try:
self._selector.register(key.fd, poller_events)
except Exception:
super().unregister(fileobj)
raise
return key return key
def unregister(self, fileobj): def unregister(self, fileobj):
key = super().unregister(fileobj) key = super().unregister(fileobj)
self._poll.unregister(key.fd) try:
self._selector.unregister(key.fd)
except OSError:
# This can happen if the FD was closed since it
# was registered.
pass
return key return key
def select(self, timeout=None): def select(self, timeout=None):
# This is shared between poll() and epoll().
# epoll() has a different signature and handling of timeout parameter.
if timeout is None: if timeout is None:
timeout = None timeout = None
elif timeout <= 0: elif timeout <= 0:
...@@ -374,14 +384,14 @@ if hasattr(select, 'poll'): ...@@ -374,14 +384,14 @@ if hasattr(select, 'poll'):
timeout = math.ceil(timeout * 1e3) timeout = math.ceil(timeout * 1e3)
ready = [] ready = []
try: try:
fd_event_list = self._poll.poll(timeout) fd_event_list = self._selector.poll(timeout)
except InterruptedError: except InterruptedError:
return ready return ready
for fd, event in fd_event_list: for fd, event in fd_event_list:
events = 0 events = 0
if event & ~select.POLLIN: if event & ~self._EVENT_READ:
events |= EVENT_WRITE events |= EVENT_WRITE
if event & ~select.POLLOUT: if event & ~self._EVENT_WRITE:
events |= EVENT_READ events |= EVENT_READ
key = self._key_from_fd(fd) key = self._key_from_fd(fd)
...@@ -390,41 +400,25 @@ if hasattr(select, 'poll'): ...@@ -390,41 +400,25 @@ if hasattr(select, 'poll'):
return ready return ready
if hasattr(select, 'epoll'): if hasattr(select, 'poll'):
class EpollSelector(_BaseSelectorImpl): class PollSelector(_PollLikeSelector):
"""Epoll-based selector.""" """Poll-based selector."""
_selector_cls = select.poll
_EVENT_READ = select.POLLIN
_EVENT_WRITE = select.POLLOUT
def __init__(self):
super().__init__()
self._epoll = select.epoll()
def fileno(self): if hasattr(select, 'epoll'):
return self._epoll.fileno()
def register(self, fileobj, events, data=None): class EpollSelector(_PollLikeSelector):
key = super().register(fileobj, events, data) """Epoll-based selector."""
epoll_events = 0 _selector_cls = select.epoll
if events & EVENT_READ: _EVENT_READ = select.EPOLLIN
epoll_events |= select.EPOLLIN _EVENT_WRITE = select.EPOLLOUT
if events & EVENT_WRITE:
epoll_events |= select.EPOLLOUT
try:
self._epoll.register(key.fd, epoll_events)
except BaseException:
super().unregister(fileobj)
raise
return key
def unregister(self, fileobj): def fileno(self):
key = super().unregister(fileobj) return self._selector.fileno()
try:
self._epoll.unregister(key.fd)
except OSError:
# This can happen if the FD was closed since it
# was registered.
pass
return key
def select(self, timeout=None): def select(self, timeout=None):
if timeout is None: if timeout is None:
...@@ -443,7 +437,7 @@ if hasattr(select, 'epoll'): ...@@ -443,7 +437,7 @@ if hasattr(select, 'epoll'):
ready = [] ready = []
try: try:
fd_event_list = self._epoll.poll(timeout, max_ev) fd_event_list = self._selector.poll(timeout, max_ev)
except InterruptedError: except InterruptedError:
return ready return ready
for fd, event in fd_event_list: for fd, event in fd_event_list:
...@@ -459,65 +453,23 @@ if hasattr(select, 'epoll'): ...@@ -459,65 +453,23 @@ if hasattr(select, 'epoll'):
return ready return ready
def close(self): def close(self):
self._epoll.close() self._selector.close()
super().close() super().close()
if hasattr(select, 'devpoll'): if hasattr(select, 'devpoll'):
class DevpollSelector(_BaseSelectorImpl): class DevpollSelector(_PollLikeSelector):
"""Solaris /dev/poll selector.""" """Solaris /dev/poll selector."""
_selector_cls = select.devpoll
def __init__(self): _EVENT_READ = select.POLLIN
super().__init__() _EVENT_WRITE = select.POLLOUT
self._devpoll = select.devpoll()
def fileno(self): def fileno(self):
return self._devpoll.fileno() return self._selector.fileno()
def register(self, fileobj, events, data=None):
key = super().register(fileobj, events, data)
poll_events = 0
if events & EVENT_READ:
poll_events |= select.POLLIN
if events & EVENT_WRITE:
poll_events |= select.POLLOUT
self._devpoll.register(key.fd, poll_events)
return key
def unregister(self, fileobj):
key = super().unregister(fileobj)
self._devpoll.unregister(key.fd)
return key
def select(self, timeout=None):
if timeout is None:
timeout = None
elif timeout <= 0:
timeout = 0
else:
# devpoll() has a resolution of 1 millisecond, round away from
# zero to wait *at least* timeout seconds.
timeout = math.ceil(timeout * 1e3)
ready = []
try:
fd_event_list = self._devpoll.poll(timeout)
except InterruptedError:
return ready
for fd, event in fd_event_list:
events = 0
if event & ~select.POLLIN:
events |= EVENT_WRITE
if event & ~select.POLLOUT:
events |= EVENT_READ
key = self._key_from_fd(fd)
if key:
ready.append((key, events & key.events))
return ready
def close(self): def close(self):
self._devpoll.close() self._selector.close()
super().close() super().close()
...@@ -528,10 +480,10 @@ if hasattr(select, 'kqueue'): ...@@ -528,10 +480,10 @@ if hasattr(select, 'kqueue'):
def __init__(self): def __init__(self):
super().__init__() super().__init__()
self._kqueue = select.kqueue() self._selector = select.kqueue()
def fileno(self): def fileno(self):
return self._kqueue.fileno() return self._selector.fileno()
def register(self, fileobj, events, data=None): def register(self, fileobj, events, data=None):
key = super().register(fileobj, events, data) key = super().register(fileobj, events, data)
...@@ -539,12 +491,12 @@ if hasattr(select, 'kqueue'): ...@@ -539,12 +491,12 @@ if hasattr(select, 'kqueue'):
if events & EVENT_READ: if events & EVENT_READ:
kev = select.kevent(key.fd, select.KQ_FILTER_READ, kev = select.kevent(key.fd, select.KQ_FILTER_READ,
select.KQ_EV_ADD) select.KQ_EV_ADD)
self._kqueue.control([kev], 0, 0) self._selector.control([kev], 0, 0)
if events & EVENT_WRITE: if events & EVENT_WRITE:
kev = select.kevent(key.fd, select.KQ_FILTER_WRITE, kev = select.kevent(key.fd, select.KQ_FILTER_WRITE,
select.KQ_EV_ADD) select.KQ_EV_ADD)
self._kqueue.control([kev], 0, 0) self._selector.control([kev], 0, 0)
except BaseException: except Exception:
super().unregister(fileobj) super().unregister(fileobj)
raise raise
return key return key
...@@ -555,7 +507,7 @@ if hasattr(select, 'kqueue'): ...@@ -555,7 +507,7 @@ if hasattr(select, 'kqueue'):
kev = select.kevent(key.fd, select.KQ_FILTER_READ, kev = select.kevent(key.fd, select.KQ_FILTER_READ,
select.KQ_EV_DELETE) select.KQ_EV_DELETE)
try: try:
self._kqueue.control([kev], 0, 0) self._selector.control([kev], 0, 0)
except OSError: except OSError:
# This can happen if the FD was closed since it # This can happen if the FD was closed since it
# was registered. # was registered.
...@@ -564,7 +516,7 @@ if hasattr(select, 'kqueue'): ...@@ -564,7 +516,7 @@ if hasattr(select, 'kqueue'):
kev = select.kevent(key.fd, select.KQ_FILTER_WRITE, kev = select.kevent(key.fd, select.KQ_FILTER_WRITE,
select.KQ_EV_DELETE) select.KQ_EV_DELETE)
try: try:
self._kqueue.control([kev], 0, 0) self._selector.control([kev], 0, 0)
except OSError: except OSError:
# See comment above. # See comment above.
pass pass
...@@ -575,7 +527,7 @@ if hasattr(select, 'kqueue'): ...@@ -575,7 +527,7 @@ if hasattr(select, 'kqueue'):
max_ev = len(self._fd_to_key) max_ev = len(self._fd_to_key)
ready = [] ready = []
try: try:
kev_list = self._kqueue.control(None, max_ev, timeout) kev_list = self._selector.control(None, max_ev, timeout)
except InterruptedError: except InterruptedError:
return ready return ready
for kev in kev_list: for kev in kev_list:
...@@ -593,7 +545,7 @@ if hasattr(select, 'kqueue'): ...@@ -593,7 +545,7 @@ if hasattr(select, 'kqueue'):
return ready return ready
def close(self): def close(self):
self._kqueue.close() self._selector.close()
super().close() super().close()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment