Skip to content

Mention in docs that asyncio.FIRST_COMPLETED does not guarantee the completion of no more than one task #83852

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
pohlt mannequin opened this issue Feb 18, 2020 · 3 comments
Labels
3.8 (EOL) end of life docs Documentation in the Doc dir topic-asyncio type-feature A feature request or enhancement

Comments

@pohlt
Copy link
Mannequin

pohlt mannequin commented Feb 18, 2020

BPO 39671
Nosy @asvetlov, @embray, @1st1, @pohlt
PRs
  • gh-83852: Clarify semantics of asyncio.wait with FIRST_COMPLETED/EXCEPTION #21918
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = None
    created_at = <Date 2020-02-18.09:33:32.173>
    labels = ['type-feature', '3.8', 'docs', 'expert-asyncio']
    title = 'Mention in docs that asyncio.FIRST_COMPLETED does not guarantee the completion of no more than one task'
    updated_at = <Date 2020-08-31.09:58:41.124>
    user = 'https://github.com/pohlt'

    bugs.python.org fields:

    activity = <Date 2020-08-31.09:58:41.124>
    actor = 'erik.bray'
    assignee = 'docs@python'
    closed = False
    closed_date = None
    closer = None
    components = ['Documentation', 'asyncio']
    creation = <Date 2020-02-18.09:33:32.173>
    creator = 'tom.pohl'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 39671
    keywords = ['patch']
    message_count = 1.0
    messages = ['362181']
    nosy_count = 5.0
    nosy_names = ['asvetlov', 'docs@python', 'erik.bray', 'yselivanov', 'tom.pohl']
    pr_nums = ['21918']
    priority = 'normal'
    resolution = None
    stage = 'patch review'
    status = 'open'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue39671'
    versions = ['Python 3.8']

    @pohlt
    Copy link
    Mannequin Author

    pohlt mannequin commented Feb 18, 2020

    Currently, the documentation of asyncio.wait gives the impression that using FIRST_COMPLETED guarantees the completion of no more than one task. In reality, the number of completed task after asyncio.wait can be larger than one.

    While this behavior (exactly one complete task if no error or cancellation occurred) would be ultimately desirable, a sentence describing the current behavior would be helpful for new users of asyncio.

    @pohlt pohlt mannequin added the 3.8 (EOL) end of life label Feb 18, 2020
    @pohlt pohlt mannequin assigned docspython Feb 18, 2020
    @pohlt pohlt mannequin added docs Documentation in the Doc dir 3.8 (EOL) end of life type-feature A feature request or enhancement labels Feb 18, 2020
    @pohlt pohlt mannequin assigned docspython Feb 18, 2020
    @pohlt pohlt mannequin added docs Documentation in the Doc dir type-feature A feature request or enhancement labels Feb 18, 2020
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    @ezio-melotti ezio-melotti moved this to Todo in asyncio Jul 17, 2022
    @mhauru
    Copy link

    mhauru commented Jun 21, 2023

    Seconding this; I'm new to asyncio and spent hours being confused by this. Here's a minimal snippet where return_when=FIRST_EXCEPTION does not return when the first exception is encountered, but rather finishes all tasks.

    import asyncio
    import time
    
    
    async def main():
        num_reps = 10
    
        async def generate():
            time.sleep(1)
            raise RuntimeError()
    
        tasks = [asyncio.create_task(generate()) for _ in range(num_reps)]
        done_tasks, pending = await asyncio.wait(tasks, return_when=asyncio.FIRST_EXCEPTION)
        print(f"Num done: {len(done_tasks)}, num pending: {len(pending)}")
    
    
    asyncio.run(main())
    

    This runs for ~10s and the last line prints Num done: 10, num pending: 0.

    @willingc
    Copy link
    Contributor

    The documentation and semantics of asyncio.wait has evolved since this issue was opened. I'm going to close it but add a reference to it in the asyncio tutorial issue make sure there are good examples for wait.

    @github-project-automation github-project-automation bot moved this from Todo to Done in asyncio Jun 21, 2024
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.8 (EOL) end of life docs Documentation in the Doc dir topic-asyncio type-feature A feature request or enhancement
    Projects
    Status: Done
    Development

    No branches or pull requests

    3 participants