Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(76425)

Unified Diff: Lib/selectors.py

Issue 21998: asyncio: a new self-pipe should be created in the child process after fork
Patch Set: Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « Lib/asyncio/unix_events.py ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
--- a/Lib/selectors.py Thu Feb 05 11:46:45 2015 +0100
+++ b/Lib/selectors.py Thu Feb 05 12:02:08 2015 +0100
@@ -192,6 +192,9 @@ class BaseSelector(metaclass=ABCMeta):
def __exit__(self, *args):
self.close()
+ def _at_fork(self):
+ pass
+
class _BaseSelectorImpl(BaseSelector):
"""Base selector implementation."""
@@ -221,6 +224,9 @@ class _BaseSelectorImpl(BaseSelector):
# Raise ValueError after all.
raise
+ def _register(self, key):
+ pass
+
def register(self, fileobj, events, data=None):
if (not events) or (events & ~(EVENT_READ | EVENT_WRITE)):
raise ValueError("Invalid events: {!r}".format(events))
@@ -231,14 +237,19 @@ class _BaseSelectorImpl(BaseSelector):
raise KeyError("{!r} (FD {}) is already registered"
.format(fileobj, key.fd))
+ self._register(key)
self._fd_to_key[key.fd] = key
return key
+ def _unregister(self, key):
+ pass
+
def unregister(self, fileobj):
try:
key = self._fd_to_key.pop(self._fileobj_lookup(fileobj))
except KeyError:
raise KeyError("{!r} is not registered".format(fileobj)) from None
+ self._unregister(key)
return key
def modify(self, fileobj, events, data=None):
@@ -286,19 +297,15 @@ class SelectSelector(_BaseSelectorImpl):
self._readers = set()
self._writers = set()
- def register(self, fileobj, events, data=None):
- key = super().register(fileobj, events, data)
- if events & EVENT_READ:
+ def _register(self, key):
+ if key.events & EVENT_READ:
self._readers.add(key.fd)
- if events & EVENT_WRITE:
+ if key.events & EVENT_WRITE:
self._writers.add(key.fd)
- return key
- def unregister(self, fileobj):
- key = super().unregister(fileobj)
+ def _unregister(self, key):
self._readers.discard(key.fd)
self._writers.discard(key.fd)
- return key
if sys.platform == 'win32':
def _select(self, r, w, _, timeout=None):
@@ -338,20 +345,16 @@ if hasattr(select, 'poll'):
super().__init__()
self._poll = select.poll()
- def register(self, fileobj, events, data=None):
- key = super().register(fileobj, events, data)
+ def _register(self, key):
poll_events = 0
- if events & EVENT_READ:
+ if key.events & EVENT_READ:
poll_events |= select.POLLIN
- if events & EVENT_WRITE:
+ if key.events & EVENT_WRITE:
poll_events |= select.POLLOUT
self._poll.register(key.fd, poll_events)
- return key
- def unregister(self, fileobj):
- key = super().unregister(fileobj)
+ def _unregister(self, key):
self._poll.unregister(key.fd)
- return key
def select(self, timeout=None):
if timeout is None:
@@ -392,25 +395,28 @@ if hasattr(select, 'epoll'):
def fileno(self):
return self._epoll.fileno()
- def register(self, fileobj, events, data=None):
- key = super().register(fileobj, events, data)
+ def _register(self, key):
epoll_events = 0
- if events & EVENT_READ:
+ if key.events & EVENT_READ:
epoll_events |= select.EPOLLIN
- if events & EVENT_WRITE:
+ if key.events & EVENT_WRITE:
epoll_events |= select.EPOLLOUT
self._epoll.register(key.fd, epoll_events)
- return key
- def unregister(self, fileobj):
- key = super().unregister(fileobj)
+ def _unregister(self, key):
try:
self._epoll.unregister(key.fd)
except OSError:
# This can happen if the FD was closed since it
# was registered.
pass
- return key
+
+ def _at_fork(self):
+ # don't unregister file descriptors: epoll is still shared with
+ # the parent process
+ self._epoll = select.epoll()
+ for key in self._fd_to_key.values():
+ self._register(key)
def select(self, timeout=None):
if timeout is None:
@@ -461,20 +467,23 @@ if hasattr(select, 'devpoll'):
def fileno(self):
return self._devpoll.fileno()
- def register(self, fileobj, events, data=None):
- key = super().register(fileobj, events, data)
+ def _register(self, key):
poll_events = 0
- if events & EVENT_READ:
+ if key.events & EVENT_READ:
poll_events |= select.POLLIN
- if events & EVENT_WRITE:
+ if key.events & EVENT_WRITE:
poll_events |= select.POLLOUT
self._devpoll.register(key.fd, poll_events)
- return key
- def unregister(self, fileobj):
- key = super().unregister(fileobj)
+ def _unregister(self, key):
self._devpoll.unregister(key.fd)
- return key
+
+ def _at_fork(self):
+ # don't unregister file descriptors: devpoll is still shared with
+ # the parent process
+ self._devpoll = select.devpoll()
+ for key in self._fd_to_key.values():
+ self._register(key)
def select(self, timeout=None):
if timeout is None:
@@ -519,20 +528,17 @@ if hasattr(select, 'kqueue'):
def fileno(self):
return self._kqueue.fileno()
- def register(self, fileobj, events, data=None):
- key = super().register(fileobj, events, data)
- if events & EVENT_READ:
+ def _register(self, key):
+ if key.events & EVENT_READ:
kev = select.kevent(key.fd, select.KQ_FILTER_READ,
select.KQ_EV_ADD)
self._kqueue.control([kev], 0, 0)
- if events & EVENT_WRITE:
+ if key.events & EVENT_WRITE:
kev = select.kevent(key.fd, select.KQ_FILTER_WRITE,
select.KQ_EV_ADD)
self._kqueue.control([kev], 0, 0)
- return key
- def unregister(self, fileobj):
- key = super().unregister(fileobj)
+ def _unregister(self, key):
if key.events & EVENT_READ:
kev = select.kevent(key.fd, select.KQ_FILTER_READ,
select.KQ_EV_DELETE)
@@ -550,7 +556,13 @@ if hasattr(select, 'kqueue'):
except OSError:
# See comment above.
pass
- return key
+
+ def _at_fork(self):
+ # don't unregister file descriptors: kqueue is still shared with
+ # the parent process
+ self._kqueue = select.kqueue()
+ for key in self._fd_to_key.values():
+ self._register(key)
def select(self, timeout=None):
timeout = None if timeout is None else max(timeout, 0)
« no previous file with comments | « Lib/asyncio/unix_events.py ('k') | no next file » | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+