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
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
Improvements to docs, including a prototype custom event loop.
  • Loading branch information
@freakboy3742
freakboy3742 committedOct 13, 2023
commit23852d0fcd3e1b1d24ecdc3132608238bbf198b2
50 changes: 31 additions & 19 deletionsDoc/library/asyncio-eventloop.rst
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -215,36 +215,48 @@ Running and stopping the loop
Set up an event loop so that it is ready to start actively looping and
processing events.

Returns the state that must be restored when the loop concludes. This state
should be passed in as arguments to :meth:`loop.run_forever_cleanup()`.

.. note::

This method is only needed if you are writing your own ``EventLoop``
subclass, with a customized inner event processing loop. For example, if
you are integrating Python's asyncio event loop with a 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 individual events. Most
end users will not need to use this method directly.
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:
Copy link
Member

Choose a reason for hiding this comment

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

It's unfortunate you need to use private (technically "protected") API and variable here.
Then again, maybe "private" is the right term and we're okay with subclasses using those.

It's also unfortunate that this documentation now constrains what we can do inrun_forever().
It almost seems that we might as well document whatrun_forever() does and specify it as never doing anything else.

break
finally:
self.run_forever_cleanup()
gui_library.cleanup()
Copy link
Member

Choose a reason for hiding this comment

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

I would imagine these two should happen in the reverse order? Cleanup in reverse order of setup. (Also, to be more robust, I'd use multiple nestedtry/finallys, but the example doesn't need that complexity.

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

I'll reverse the order; agreed that another layer of try/finally is probably overkill for documenation purposes.


.. versionadded:: 3.13

.. method:: loop.run_forever_cleanup(original_state)
.. 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.
Copy link
Member

Choose a reason for hiding this comment

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

Maybe say something about whether this is re-entrant? Should I worry about not calling it twice?


The *original_state* argument is the return value provided by the call to
:meth:`loop.run_forever_setup()` that was used to set up the event loop.

.. note::

This method is only needed if you are writing your own ``EventLoop``
subclass, with a customized inner event processing loop. For example, if
you are integrating Python's asyncio event loop with a GUI library's event
loop, you can use this method to ensure that Python's event loop has been
fully shut down at the conclusion of processing events. Most end users
will not need to use this method directly.
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

Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp