summaryrefslogtreecommitdiffstats
path: root/Lib/asyncio
diff options
context:
space:
mode:
authorJ. Nick Koston <nick@koston.org>2023-07-22 21:07:40 (GMT)
committerGitHub <noreply@github.com>2023-07-22 21:07:40 (GMT)
commitb7dc795dfd175c0d25a479cfaf94a13c368a5a7b (patch)
tree1a0216ed11f687c382c05e543a2b982ea97e963d /Lib/asyncio
parentee15844db88fab3a282d14325662bcfd026272ac (diff)
downloadcpython-b7dc795dfd175c0d25a479cfaf94a13c368a5a7b.zip
cpython-b7dc795dfd175c0d25a479cfaf94a13c368a5a7b.tar.gz
cpython-b7dc795dfd175c0d25a479cfaf94a13c368a5a7b.tar.bz2
gh-106527: asyncio: optimize to add/remove readers and writers (#106528)
Diffstat (limited to 'Lib/asyncio')
-rw-r--r--Lib/asyncio/selector_events.py64
1 files changed, 29 insertions, 35 deletions
diff --git a/Lib/asyncio/selector_events.py b/Lib/asyncio/selector_events.py
index f895750..d521b4e 100644
--- a/Lib/asyncio/selector_events.py
+++ b/Lib/asyncio/selector_events.py
@@ -274,9 +274,8 @@ class BaseSelectorEventLoop(base_events.BaseEventLoop):
def _add_reader(self, fd, callback, *args):
self._check_closed()
handle = events.Handle(callback, args, self, None)
- try:
- key = self._selector.get_key(fd)
- except KeyError:
+ key = self._selector.get_map().get(fd)
+ if key is None:
self._selector.register(fd, selectors.EVENT_READ,
(handle, None))
else:
@@ -290,30 +289,27 @@ class BaseSelectorEventLoop(base_events.BaseEventLoop):
def _remove_reader(self, fd):
if self.is_closed():
return False
- try:
- key = self._selector.get_key(fd)
- except KeyError:
+ key = self._selector.get_map().get(fd)
+ if key is None:
return False
+ mask, (reader, writer) = key.events, key.data
+ mask &= ~selectors.EVENT_READ
+ if not mask:
+ self._selector.unregister(fd)
else:
- mask, (reader, writer) = key.events, key.data
- mask &= ~selectors.EVENT_READ
- if not mask:
- self._selector.unregister(fd)
- else:
- self._selector.modify(fd, mask, (None, writer))
+ self._selector.modify(fd, mask, (None, writer))
- if reader is not None:
- reader.cancel()
- return True
- else:
- return False
+ if reader is not None:
+ reader.cancel()
+ return True
+ else:
+ return False
def _add_writer(self, fd, callback, *args):
self._check_closed()
handle = events.Handle(callback, args, self, None)
- try:
- key = self._selector.get_key(fd)
- except KeyError:
+ key = self._selector.get_map().get(fd)
+ if key is None:
self._selector.register(fd, selectors.EVENT_WRITE,
(None, handle))
else:
@@ -328,24 +324,22 @@ class BaseSelectorEventLoop(base_events.BaseEventLoop):
"""Remove a writer callback."""
if self.is_closed():
return False
- try:
- key = self._selector.get_key(fd)
- except KeyError:
+ key = self._selector.get_map().get(fd)
+ if key is None:
return False
+ mask, (reader, writer) = key.events, key.data
+ # Remove both writer and connector.
+ mask &= ~selectors.EVENT_WRITE
+ if not mask:
+ self._selector.unregister(fd)
else:
- mask, (reader, writer) = key.events, key.data
- # Remove both writer and connector.
- mask &= ~selectors.EVENT_WRITE
- if not mask:
- self._selector.unregister(fd)
- else:
- self._selector.modify(fd, mask, (reader, None))
+ self._selector.modify(fd, mask, (reader, None))
- if writer is not None:
- writer.cancel()
- return True
- else:
- return False
+ if writer is not None:
+ writer.cancel()
+ return True
+ else:
+ return False
def add_reader(self, fd, callback, *args):
"""Add a reader callback."""