FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

asyncio.tasks.wait does not allow to set custom exception when return_when=FIRST_EXCEPTION · Issue #77584 · python/cpython · GitHub

Repository navigation

asyncio.tasks.wait does not allow to set custom exception when return_when=FIRST_EXCEPTION #77584

Closed

Description

mannequin
BPO 33403
Nosy @giampaolo, @hniksic, @asvetlov, @1st1, @pyneda
PRs
  • bpo-33403: Added return_on parameter to asyncio.tasks.wait coro #6679
  • 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 2018-05-02.00:33:54.589>
    labels = ['type-feature', '3.8', 'expert-asyncio']
    title = 'asyncio.tasks.wait does not allow to set custom exception when return_when=FIRST_EXCEPTION'
    updated_at = <Date 2018-05-17.16:36:46.486>
    user = 'https://github.com/pyneda'

    bugs.python.org fields:

    activity = <Date 2018-05-17.16:36:46.486>
    actor = 'yselivanov'
    assignee = 'none'
    closed = False
    closed_date = None
    closer = None
    components = ['asyncio']
    creation = <Date 2018-05-02.00:33:54.589>
    creator = 'pyneda'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 33403
    keywords = ['patch']
    message_count = 3.0
    messages = ['316761', '316866', '316961']
    nosy_count = 5.0
    nosy_names = ['giampaolo.rodola', 'hniksic', 'asvetlov', 'yselivanov', 'pyneda']
    pr_nums = ['6679']
    priority = 'normal'
    resolution = None
    stage = 'patch review'
    status = 'open'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue33403'
    versions = ['Python 3.8']

    Activity

    1. pyneda commented on May 16, 2018

      pynedamannequin
      MannequinAuthor

      A possible use case (that at least I couldn't find how to solve) is the possibility to cancel a bunch of futures/coroutine objects which are being awaited using asyncio.tasks.wait() by one of the running coroutines when a task succeded or a specific condition happens by just raising an specific exception.

      At the moment this can be done but it will cancel all the coroutines with any exception that is raised and at some occasions this may not be desired.

      A simple example:

      async def example(num):
          if x == 5:
              raise Exception('Exception that does not cancel')
          elif x == 15:
              raise CancelException()
      
      tasks = [asyncio.ensure_future(example(x)) for x in range(20)]
      done, pending = await asyncio.wait(tasks, return_when=FIRST_EXCEPTION, return_on=CancelException)

      This wouldn't cancel everything when a normal exception is raised, instead it will when the exception raised is the one that the user expects to be raised in order to cancel everything that is pending.

      In addition, if the user does not specify the Exception type, it uses default Exception so it would keep working exactly as now.

    2. hniksic commented on May 16, 2018

      hniksicmannequin
      Mannequin

      """
      At the moment this can be done but it will cancel all the coroutines with any exception that is raised and at some occasions this may not be desired.
      """

      Does wait() really "cancel all the coroutines"? The documentation doesn't mention wait() canceling anything it only returns them in the pending set. It is gather() and wait_for() that cancel automatically.

      If you want to cancel everything on some exception and not on another, you can easily implement the logic yourself, e.g:

      tasks = [asyncio.ensure_future(example(x)) for x in range(20)]
      done, pending = await asyncio.wait(tasks, return_when=FIRST_EXCEPTION)
      for fut in done:
          try:
              fut.result()
          except CancelException:
              for fut in pending:
                  fut.cancel()
    3. 1st1 commented on May 17, 2018

      Member

      This isn't a priority now, so let's postpone the discussion on this until 3.7 is released.

    4. transferred this issue fromon Apr 10, 2022
    5. asvetlov commented on Nov 13, 2024

      Contributor

      Thanks for the proposal, but I'm closing the issue.

      asyncio develops in a slightly different way; task.cancel() accepts an optional msg parameter that could be utilized by user's code.

    6. moved this from Todo to Done in asyncioon Nov 13, 2024
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Projects

      • Status
        Done

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions


      Back | FazBrowse Home | New Git URL