Skip to content

Commit

Permalink
Keep the setup/cleanup methods as protected API.
Browse files Browse the repository at this point in the history
  • Loading branch information
freakboy3742 committed Oct 13, 2023
1 parent f944944 commit 3143126
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 66 deletions.
50 changes: 0 additions & 50 deletions Doc/library/asyncio-eventloop.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
^^^^^^^^^^^^^^^^^^^^

Expand Down
20 changes: 10 additions & 10 deletions Lib/asyncio/base_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
Expand All @@ -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.
Expand Down
8 changes: 4 additions & 4 deletions Lib/asyncio/windows_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions Lib/test/test_asyncio/test_base_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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):
Expand Down

0 comments on commit 3143126

Please sign in to comment.