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):
return ready
if hasattr(select, 'poll'):
class PollSelector(_BaseSelectorImpl):
"""Poll-based selector."""
class _PollLikeSelector(_BaseSelectorImpl):
"""Base class shared between poll, epoll and devpoll selectors."""
_selector_cls = None
def __init__(self):
super().__init__()
self._poll = select.poll()
self._selector = self._selector_cls()
def register(self, fileobj, events, data=None):
key = super().register(fileobj, events, data)
poll_events = 0
poller_events = 0
if events & EVENT_READ:
poll_events |= select.POLLIN
poller_events |= self._EVENT_READ
if events & EVENT_WRITE:
poll_events |= select.POLLOUT
self._poll.register(key.fd, poll_events)
poller_events |= self._EVENT_WRITE
try:
self._selector.register(key.fd, poller_events)
except Exception:
super().unregister(fileobj)
raise
return key
def unregister(self, 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
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:
timeout = None
elif timeout <= 0:
......@@ -374,14 +384,14 @@ if hasattr(select, 'poll'):
timeout = math.ceil(timeout * 1e3)
ready = []
try:
fd_event_list = self._poll.poll(timeout)
fd_event_list = self._selector.poll(timeout)
except InterruptedError:
return ready
for fd, event in fd_event_list:
events = 0
if event & ~select.POLLIN:
if event & ~self._EVENT_READ:
events |= EVENT_WRITE
if event & ~select.POLLOUT:
if event & ~self._EVENT_WRITE:
events |= EVENT_READ
key = self._key_from_fd(fd)
......@@ -390,41 +400,25 @@ if hasattr(select, 'poll'):
return ready
if hasattr(select, 'epoll'):
if hasattr(select, 'poll'):
class EpollSelector(_BaseSelectorImpl):
"""Epoll-based selector."""
class PollSelector(_PollLikeSelector):
"""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):
return self._epoll.fileno()
if hasattr(select, 'epoll'):
def register(self, fileobj, events, data=None):
key = super().register(fileobj, events, data)
epoll_events = 0
if events & EVENT_READ:
epoll_events |= select.EPOLLIN
if events & EVENT_WRITE:
epoll_events |= select.EPOLLOUT
try:
self._epoll.register(key.fd, epoll_events)
except BaseException:
super().unregister(fileobj)
raise
return key
class EpollSelector(_PollLikeSelector):
"""Epoll-based selector."""
_selector_cls = select.epoll
_EVENT_READ = select.EPOLLIN
_EVENT_WRITE = select.EPOLLOUT
def unregister(self, fileobj):
key = super().unregister(fileobj)
try:
self._epoll.unregister(key.fd)
except OSError:
# This can happen if the FD was closed since it
# was registered.
pass
return key
def fileno(self):
return self._selector.fileno()
def select(self, timeout=None):
if timeout is None:
......@@ -443,7 +437,7 @@ if hasattr(select, 'epoll'):
ready = []
try:
fd_event_list = self._epoll.poll(timeout, max_ev)
fd_event_list = self._selector.poll(timeout, max_ev)
except InterruptedError:
return ready
for fd, event in fd_event_list:
......@@ -459,65 +453,23 @@ if hasattr(select, 'epoll'):
return ready
def close(self):
self._epoll.close()
self._selector.close()
super().close()
if hasattr(select, 'devpoll'):
class DevpollSelector(_BaseSelectorImpl):
class DevpollSelector(_PollLikeSelector):
"""Solaris /dev/poll selector."""
def __init__(self):
super().__init__()
self._devpoll = select.devpoll()
_selector_cls = select.devpoll
_EVENT_READ = select.POLLIN
_EVENT_WRITE = select.POLLOUT
def fileno(self):
return self._devpoll.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
return self._selector.fileno()
def close(self):
self._devpoll.close()
self._selector.close()
super().close()
......@@ -528,10 +480,10 @@ if hasattr(select, 'kqueue'):
def __init__(self):
super().__init__()
self._kqueue = select.kqueue()
self._selector = select.kqueue()
def fileno(self):
return self._kqueue.fileno()
return self._selector.fileno()
def register(self, fileobj, events, data=None):
key = super().register(fileobj, events, data)
......@@ -539,12 +491,12 @@ if hasattr(select, 'kqueue'):
if events & EVENT_READ:
kev = select.kevent(key.fd, select.KQ_FILTER_READ,
select.KQ_EV_ADD)
self._kqueue.control([kev], 0, 0)
self._selector.control([kev], 0, 0)
if events & EVENT_WRITE:
kev = select.kevent(key.fd, select.KQ_FILTER_WRITE,
select.KQ_EV_ADD)
self._kqueue.control([kev], 0, 0)
except BaseException:
self._selector.control([kev], 0, 0)
except Exception:
super().unregister(fileobj)
raise
return key
......@@ -555,7 +507,7 @@ if hasattr(select, 'kqueue'):
kev = select.kevent(key.fd, select.KQ_FILTER_READ,
select.KQ_EV_DELETE)
try:
self._kqueue.control([kev], 0, 0)
self._selector.control([kev], 0, 0)
except OSError:
# This can happen if the FD was closed since it
# was registered.
......@@ -564,7 +516,7 @@ if hasattr(select, 'kqueue'):
kev = select.kevent(key.fd, select.KQ_FILTER_WRITE,
select.KQ_EV_DELETE)
try:
self._kqueue.control([kev], 0, 0)
self._selector.control([kev], 0, 0)
except OSError:
# See comment above.
pass
......@@ -575,7 +527,7 @@ if hasattr(select, 'kqueue'):
max_ev = len(self._fd_to_key)
ready = []
try:
kev_list = self._kqueue.control(None, max_ev, timeout)
kev_list = self._selector.control(None, max_ev, timeout)
except InterruptedError:
return ready
for kev in kev_list:
......@@ -593,7 +545,7 @@ if hasattr(select, 'kqueue'):
return ready
def close(self):
self._kqueue.close()
self._selector.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