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
NextNext commit
Decompose run_forever() into parts to allow external usage.
  • Loading branch information
@freakboy3742
freakboy3742 committedOct 12, 2023
commita0c1fde9ec0bec2582400156bca37ad111eccb5b
54 changes: 42 additions & 12 deletionsLib/asyncio/base_events.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -601,29 +601,59 @@ def _check_running(self):
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):
"""Set up an event loop so that it is ready to start actively looping
to process events.
Copy link
Member

Choose a reason for hiding this comment

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

By convention the first paragraph of a docstring must be a one-line summary.

Suggested change
"""Setupaneventloopsothatitisreadytostartactivelylooping
toprocessevents.
"""Prepareforprocessingevents.

freakboy3742 and willingc reacted with thumbs up emoji

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

Choose a reason for hiding this comment

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

Document the type of the state, please. (If it's meant to be opaque, make it more opaque than a 1-tuple please. :-)

I find it a bit odd that the method name is "run_forever_setup()", which doesn't hint at the existence of a return value. (Could you store the state to be restored in a private/protected instance variable instead of returning it?)

Also, "arguments"? Or "argument"?

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

I've modified the implementation to store the state as a protected variable, which simplifies the interface.


This method is only needed if you are writing your own event loop, with
Copy link
Member

Choose a reason for hiding this comment

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

"is only needed if" -- odd phrasing. The method exists. Do you mean it only needs to be overridden? Maybe show a brief example of how it's supposed to be used in that case? (I intentionally didn't the docs before reading the code, and from only reading the docstring I'm a bit confused about what's hooking what. From reading more code I realize that setup/cleanup are what you intend to override.)

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

No - it would be run_forever() that is being replaced. I've provided a sample implementation in the docs; I'll clarify the language here to (hopefully) make the intended usage more clear.

customized ``run_forever`` semantics (e.g., integrating a GUI event loop
with Python's event loop).
"""
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._thread_id = threading.get_ident()
sys.set_asyncgen_hooks(
firstiter=self._asyncgen_firstiter_hook,
finalizer=self._asyncgen_finalizer_hook
)

events._set_running_loop(self)

return (old_agen_hooks,)

events._set_running_loop(self)
def run_forever_cleanup(self, orig_state):
"""Clean up an event loop after the event loop finishes the looping over
events.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
"""Cleanupaneventloopaftertheeventloopfinishestheloopingover
events.
"""Cleanupaftertheeventloopfinishestheloopingoverevents.


Restores any state preserved by ``run_forever_setup()``.

This method is only needed if you are writing your own event loop, with
customized ``run_forever`` semantics (e.g., integrating a GUI event loop
with Python's event loop).
"""
old_agen_hooks, = orig_state
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)

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:
orig_state = 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(orig_state)

def run_until_complete(self, future):
"""Run until the Future is done.
Expand Down
37 changes: 19 additions & 18 deletionsLib/asyncio/windows_events.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -314,24 +314,25 @@ def __init__(self, proactor=None):
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)

return super().run_forever_setup()

def run_forever_cleanup(self, orig_state):
Copy link
Member

Choose a reason for hiding this comment

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

Why is there no super call here?

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

Definitley an oversight (which the test suite has picked up). A correction is incoming.

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)
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp