diff --git a/Doc/library/asyncio-eventloop.rst b/Doc/library/asyncio-eventloop.rst index 21025079e0f8c9..361e7bb9c8f2fa 100644 --- a/Doc/library/asyncio-eventloop.rst +++ b/Doc/library/asyncio-eventloop.rst @@ -210,56 +210,6 @@ Running and stopping the loop .. versionchanged:: 3.12 Added the *timeout* parameter. -.. method:: loop.run_forever_setup() - - Set up an event loop so that it is ready to start actively looping and - processing events. - - .. note:: - - End users should not use this method directly. This method is only needed - if you are writing your own ``EventLoop`` subclass, with a customized - event processing loop. For example, if you are integrating Python's - asyncio event loop with a GUI library's event loop, you may need to write - a customized :meth:`loop.run_forever` implementation that accommodates - both CPython's event loop and the GUI library's event loop. You can use - this method to ensure that Python's event loop is correctly configured and - ready to start processing events. - - The specific details of a customized ``EventLoop`` subclass will depend - on the GUI library you are integrating with. However, the broad structure - of a custom ``EventLoop`` would look something like:: - - class CustomGUIEventLoop(EventLoop): - def run_forever(self): - try: - self.run_forever_setup() - gui_library.setup() - while True: - self._run_once() - gui_library.process_events() - if self._stopping: - break - finally: - self.run_forever_cleanup() - gui_library.cleanup() - - .. versionadded:: 3.13 - -.. method:: loop.run_forever_cleanup() - - Perform any cleanup necessary at the conclusion of event processing to ensure - that the event loop has been fully shut down. - - .. note:: - - End users should not use this method directly. This method is only needed - if you are writing your own ``EventLoop`` subclass, with a customized - inner event processing loop. See :meth:`loop.run_forever_setup()` for - details on why and how to use this method. - - .. versionadded:: 3.13 - Scheduling callbacks ^^^^^^^^^^^^^^^^^^^^ diff --git a/Lib/asyncio/base_events.py b/Lib/asyncio/base_events.py index 88ef4b5ba15aef..0476de631a6a52 100644 --- a/Lib/asyncio/base_events.py +++ b/Lib/asyncio/base_events.py @@ -603,12 +603,12 @@ def _check_running(self): raise RuntimeError( 'Cannot run the event loop while another loop is running') - def run_forever_setup(self): + def _run_forever_setup(self): """Prepare the run loop to process events. - This method should be used as part of the ``run_forever()`` - implementation in a custom event loop subclass (e.g., integrating a GUI - event loop with Python's event loop). + 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() @@ -623,12 +623,12 @@ def run_forever_setup(self): events._set_running_loop(self) - def run_forever_cleanup(self): + def _run_forever_cleanup(self): """Clean up after an event loop finishes the looping over events. - This method should be used as part of the ``run_forever()`` - implementation in a custom event loop subclass (e.g., integrating a GUI - event loop with Python's event loop). + 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 @@ -642,13 +642,13 @@ def run_forever_cleanup(self): def run_forever(self): """Run until stop() is called.""" try: - self.run_forever_setup() + self._run_forever_setup() while True: self._run_once() if self._stopping: break finally: - self.run_forever_cleanup() + 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 7f7fcb9f9014b8..b62ea75fee3858 100644 --- a/Lib/asyncio/windows_events.py +++ b/Lib/asyncio/windows_events.py @@ -314,13 +314,13 @@ def __init__(self, proactor=None): proactor = IocpProactor() super().__init__(proactor) - def run_forever_setup(self): + def _run_forever_setup(self): assert self._self_reading_future is None self.call_soon(self._loop_self_reading) - super().run_forever_setup() + super()._run_forever_setup() - def run_forever_cleanup(self): - super().run_forever_cleanup() + 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() diff --git a/Lib/test/test_asyncio/test_base_events.py b/Lib/test/test_asyncio/test_base_events.py index ce0a81ad7f4c04..c2080977e9d587 100644 --- a/Lib/test/test_asyncio/test_base_events.py +++ b/Lib/test/test_asyncio/test_base_events.py @@ -936,7 +936,7 @@ def callback(): self.loop.call_soon(callback) # Set up the custom event loop - self.loop.run_forever_setup() + self.loop._run_forever_setup() # Confirm the loop has been started self.assertEqual(asyncio.get_running_loop(), self.loop) @@ -947,7 +947,7 @@ def callback(): self.loop._run_once() # Clean up the event loop - self.loop.run_forever_cleanup() + self.loop._run_forever_cleanup() # Confirm the loop has been cleaned up with self.assertRaises(RuntimeError):