Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gh-110771: Decompose run_forever() into parts#110773

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
gvanrossum merged 12 commits intopython:mainfromfreakboy3742:decomposed-event-loop
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from1 commit
Commits
Show all changes
12 commits
Select commitHold shift + click to select a range
a0c1fde
Decompose run_forever() into parts to allow external usage.
freakboy3742Oct 12, 2023
67bfc8f
📜🤖 Added by blurb_it.
blurb-it[bot]Oct 13, 2023
3dcf97b
Add docs for run_forever_setup and run_forever_cleanup.
freakboy3742Oct 13, 2023
35271dc
Ensure failures during setup don't prevent cleanup.
freakboy3742Oct 13, 2023
6672f68
Add tests for custom event loop implementation.
freakboy3742Oct 13, 2023
54349c3
Store pre-event loop state as a protected variable.
freakboy3742Oct 13, 2023
e6226a5
Add a missing super() call.
freakboy3742Oct 13, 2023
23852d0
Improvements to docs, including a prototype custom event loop.
freakboy3742Oct 13, 2023
e7f892e
Correct prototype of run_forever_cleanup on Windows Proactor.
freakboy3742Oct 13, 2023
f944944
Merge branch 'main' into decomposed-event-loop
freakboy3742Oct 13, 2023
3143126
Keep the setup/cleanup methods as protected API.
freakboy3742Oct 13, 2023
1b3e3d8
Merge branch 'main' into decomposed-event-loop
freakboy3742Oct 13, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
PrevPrevious commit
NextNext commit
Keep the setup/cleanup methods as protected API.
  • Loading branch information
@freakboy3742
freakboy3742 committedOct 13, 2023
commit3143126de9b18902ddc3767c86796fc6f5660e79
50 changes: 0 additions & 50 deletionsDoc/library/asyncio-eventloop.rst
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionsLib/asyncio/base_events.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -603,12 +603,12 @@ def _check_running(self):
raise RuntimeError(
'Cannot run the event loop while another loop is running')

defrun_forever_setup(self):
def_run_forever_setup(self):
"""Prepare the run loop to process events.

This methodshould be used as part of the ``run_forever()``
implementation in acustom event loopsubclass (e.g., integrating a GUI
eventloopwith Python's event loop).
This methodexists so that custom custom event loop subclasses (e.g., event loops
that integrate aGUI event loopwith Python's event loop) have access to all the
loopsetup logic.
"""
self._check_closed()
self._check_running()
Expand All@@ -623,12 +623,12 @@ def run_forever_setup(self):

events._set_running_loop(self)

defrun_forever_cleanup(self):
def_run_forever_cleanup(self):
"""Clean up after an event loop finishes the looping over events.

This methodshould be used as part of the ``run_forever()``
implementation in acustom event loopsubclass (e.g., integrating a GUI
eventloopwith Python's event loop).
This methodexists so that custom custom event loop subclasses (e.g., event loops
that integrate aGUI event loopwith Python's event loop) have access to all the
loopcleanup 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."""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Maybe clarify in this docstring that this is what calls setup/cleanup?

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 deletionsLib/asyncio/windows_events.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -314,13 +314,13 @@ def __init__(self, proactor=None):
proactor = IocpProactor()
super().__init__(proactor)

defrun_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()

defrun_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 deletionsLib/test/test_asyncio/test_base_events.py
View file
Open in desktop
Original file line numberDiff line numberDiff 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

[8]ページ先頭

©2009-2025 Movatter.jp