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-129195: use future_add_to_awaited_by/future_discard_from_awaited_by in asyncio.staggered.staggered_race#129253

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

Conversation

@graingert
Copy link
Contributor

@graingertgraingert commentedJan 24, 2025
edited by bedevere-appbot
Loading

@graingertgraingertforce-pushed thestaggered-race-add-to-awaited-by branch fromddeeee9 to7626ae4CompareJanuary 24, 2025 10:43
"""
# TODO: when we have aiter() and anext(), allow async iterables in coro_fns.
loop=looporevents.get_running_loop()
parent_task=tasks.current_task(loop)

Choose a reason for hiding this comment

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

What happens if we're not running in a task? Should we assert that this is non-None?

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

one possible outcome is that there's no task or running loop:

importasyncio.staggeredasyncdefmain():asyncdefasyncfn():passawaitasyncio.staggered.staggered_race([asyncfn,asyncfn],delay=None)main().__await__().send(None)

we get a nice traceback, saying that:

./python demo.pyTraceback (most recent call last):  File"/home/graingert/projects/cpython/demo.py", line9, in<module>    main().__await__().send(None)~~~~~~~~~~~~~~~~~~~~~~~^^^^^^  File"/home/graingert/projects/cpython/demo.py", line7, inmainawait asyncio.staggered.staggered_race([asyncfn, asyncfn],delay=None)  File"/home/graingert/projects/cpython/Lib/asyncio/staggered.py", line66, instaggered_race    loop= loopor events.get_running_loop()~~~~~~~~~~~~~~~~~~~~~~~^^RuntimeError:no running event loop

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

if you manually pass a loop, and step the coroutine outside of a task:

importasyncio.staggeredasyncdefmain(loop):asyncdefasyncfn():print("hello")awaitasyncio.staggered.staggered_race([asyncfn,asyncfn],delay=None,loop=loop)return"world"loop=asyncio.EventLoop()coro=main(loop).__await__()f=coro.send(None)loop.run_until_complete(f)try:coro.send(None)exceptStopIterationase:print(e.value)

by some miracle it all works, this is because future_add_to_awaited_by and future_discard_from_awaited_by is noop if any arg is not a future (eg is None) and we don't have any further use of the parent task

./pythondemo.pyhelloworld

so we should probably leave it like that.

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

I don't think it's worth testing this usecase though

ZeroIntensity reacted with thumbs up emoji
t.cancel()

propagate_cancellation_error=None
try:

Choose a reason for hiding this comment

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

Above this--for cancelling all the coroutines,future_discard_from_awaited_by should get called, right?

Copy link
ContributorAuthor

@graingertgraingertJan 24, 2025
edited
Loading

Choose a reason for hiding this comment

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

Yeah add_done_callback callbacks are called on cancellation, so it will get called

ZeroIntensity reacted with thumbs up emoji
…9d5NU.rstCo-authored-by: Kumar Aditya <kumaraditya@python.org>
Copy link
Member

@ZeroIntensityZeroIntensity left a comment

Choose a reason for hiding this comment

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

LGTM as well.

@kumaraditya303kumaraditya303 merged commitfccbfc4 intopython:mainJan 26, 2025
38 checks passed
@graingertgraingert deleted the staggered-race-add-to-awaited-by branchJanuary 26, 2025 15:44
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@ZeroIntensityZeroIntensityZeroIntensity approved these changes

@kumaraditya303kumaraditya303kumaraditya303 approved these changes

@1st11st1Awaiting requested review from 1st11st1 is a code owner

@asvetlovasvetlovAwaiting requested review from asvetlovasvetlov is a code owner

@willingcwillingcAwaiting requested review from willingcwillingc is a code owner

@ambvambvAwaiting requested review from ambv

Assignees

No one assigned

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

3 participants

@graingert@ZeroIntensity@kumaraditya303

[8]ページ先頭

©2009-2025 Movatter.jp