summaryrefslogtreecommitdiffstats
path: root/Lib/asyncio
diff options
context:
space:
mode:
authorRussell Keith-Magee <russell@keith-magee.com>2023-10-13 14:12:32 (GMT)
committerGitHub <noreply@github.com>2023-10-13 14:12:32 (GMT)
commita7e2a10a85bb597d3bb8f9303214bd0524fa54c3 (patch)
tree9767640a38dfaa2e297492a45ab13030830443bd /Lib/asyncio
parent0ed2329a1627fc8ae97b009114cd960c25567f75 (diff)
downloadcpython-a7e2a10a85bb597d3bb8f9303214bd0524fa54c3.zip
cpython-a7e2a10a85bb597d3bb8f9303214bd0524fa54c3.tar.gz
cpython-a7e2a10a85bb597d3bb8f9303214bd0524fa54c3.tar.bz2
gh-110771: Decompose run_forever() into parts (#110773)
Effectively introduce an unstable, private (really: protected) API for subclasses that want to override `run_forever()`.
Diffstat (limited to 'Lib/asyncio')
-rw-r--r--Lib/asyncio/base_events.py51
-rw-r--r--Lib/asyncio/windows_events.py37
2 files changed, 57 insertions, 31 deletions
diff --git a/Lib/asyncio/base_events.py b/Lib/asyncio/base_events.py
index 956864e..0476de6 100644
--- a/Lib/asyncio/base_events.py
+++ b/Lib/asyncio/base_events.py
@@ -400,6 +400,8 @@ class BaseEventLoop(events.AbstractEventLoop):
self._clock_resolution = time.get_clock_info('monotonic').resolution
self._exception_handler = None
self.set_debug(coroutines._is_debug_mode())
+ # The preserved state of async generator hooks.
+ self._old_agen_hooks = None
# In debug mode, if the execution of a callback or a step of a task
# exceed this duration in seconds, the slow callback/task is logged.
self.slow_callback_duration = 0.1
@@ -601,29 +603,52 @@ class BaseEventLoop(events.AbstractEventLoop):
raise RuntimeError(
'Cannot run the event loop while another loop is running')
- def run_forever(self):
- """Run until stop() is called."""
+ def _run_forever_setup(self):
+ """Prepare the run loop to process events.
+
+ This method exists so that custom custom event loop subclasses (e.g., event loops
+ that integrate a GUI event loop with Python's event loop) have access to all the
+ loop setup logic.
+ """
self._check_closed()
self._check_running()
self._set_coroutine_origin_tracking(self._debug)
- old_agen_hooks = sys.get_asyncgen_hooks()
- try:
- self._thread_id = threading.get_ident()
- sys.set_asyncgen_hooks(firstiter=self._asyncgen_firstiter_hook,
- finalizer=self._asyncgen_finalizer_hook)
+ self._old_agen_hooks = sys.get_asyncgen_hooks()
+ self._thread_id = threading.get_ident()
+ sys.set_asyncgen_hooks(
+ firstiter=self._asyncgen_firstiter_hook,
+ finalizer=self._asyncgen_finalizer_hook
+ )
+
+ events._set_running_loop(self)
+
+ def _run_forever_cleanup(self):
+ """Clean up after an event loop finishes the looping over events.
- events._set_running_loop(self)
+ This method exists so that custom custom event loop subclasses (e.g., event loops
+ that integrate a GUI event loop with Python's event loop) have access to all the
+ loop cleanup logic.
+ """
+ self._stopping = False
+ self._thread_id = None
+ events._set_running_loop(None)
+ self._set_coroutine_origin_tracking(False)
+ # Restore any pre-existing async generator hooks.
+ if self._old_agen_hooks is not None:
+ sys.set_asyncgen_hooks(*self._old_agen_hooks)
+ self._old_agen_hooks = None
+
+ def run_forever(self):
+ """Run until stop() is called."""
+ try:
+ self._run_forever_setup()
while True:
self._run_once()
if self._stopping:
break
finally:
- self._stopping = False
- self._thread_id = None
- events._set_running_loop(None)
- self._set_coroutine_origin_tracking(False)
- sys.set_asyncgen_hooks(*old_agen_hooks)
+ self._run_forever_cleanup()
def run_until_complete(self, future):
"""Run until the Future is done.
diff --git a/Lib/asyncio/windows_events.py b/Lib/asyncio/windows_events.py
index 4a4c4be..b62ea75 100644
--- a/Lib/asyncio/windows_events.py
+++ b/Lib/asyncio/windows_events.py
@@ -314,24 +314,25 @@ class ProactorEventLoop(proactor_events.BaseProactorEventLoop):
proactor = IocpProactor()
super().__init__(proactor)
- def run_forever(self):
- try:
- assert self._self_reading_future is None
- self.call_soon(self._loop_self_reading)
- super().run_forever()
- finally:
- if self._self_reading_future is not None:
- ov = self._self_reading_future._ov
- self._self_reading_future.cancel()
- # self_reading_future was just cancelled so if it hasn't been
- # finished yet, it never will be (it's possible that it has
- # already finished and its callback is waiting in the queue,
- # where it could still happen if the event loop is restarted).
- # Unregister it otherwise IocpProactor.close will wait for it
- # forever
- if ov is not None:
- self._proactor._unregister(ov)
- self._self_reading_future = None
+ def _run_forever_setup(self):
+ assert self._self_reading_future is None
+ self.call_soon(self._loop_self_reading)
+ super()._run_forever_setup()
+
+ def _run_forever_cleanup(self):
+ super()._run_forever_cleanup()
+ if self._self_reading_future is not None:
+ ov = self._self_reading_future._ov
+ self._self_reading_future.cancel()
+ # self_reading_future was just cancelled so if it hasn't been
+ # finished yet, it never will be (it's possible that it has
+ # already finished and its callback is waiting in the queue,
+ # where it could still happen if the event loop is restarted).
+ # Unregister it otherwise IocpProactor.close will wait for it
+ # forever
+ if ov is not None:
+ self._proactor._unregister(ov)
+ self._self_reading_future = None
async def create_pipe_connection(self, protocol_factory, address):
f = self._proactor.connect_pipe(address)