8000 uasyncio gather() return_exceptions behaviour differs from CPython · Issue #7807 · micropython/micropython · GitHub
[go: up one dir, main page]

Skip to content
uasyncio gather() return_exceptions behaviour differs from CPython #7807
Closed
@peterhinch

Description

@peterhinch

I think this is a regression. The following script produces an unexpected result in MicroPython:

try:
    import uasyncio as asyncio
except ImportError:
    import asyncio

async def will_stop(n):
    print('Runs to completion')
    for _ in range(6):
        await asyncio.sleep(1)
    return 2 * n

async def forever(n):
    print('forever() will be cancelled')
    while True:
        await asyncio.sleep(1)
        n += 1
    return n

async def do_cancel(task):
    await asyncio.sleep(3)
    print('About to cancel forever')
    task.cancel()

async def main():
    tasks = [asyncio.create_task(forever(70))]
    tasks.append(will_stop(21))
    asyncio.create_task(do_cancel(tasks[0]))
    res = None
    try:
        res = await asyncio.gather(*tasks, return_exceptions=True)
    except asyncio.CancelledError:
        print('Cancelled')
    print('Result: ', res)

asyncio.run(main())

CPython (expected behaviour):

adminpete@debian8:/mnt/qnap2/temp$ python3
Python 3.8.0 (default, Dec  6 2019, 16:20:13) 
[GCC 4.9.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import rats40
forever() will be cancelled
Runs to completion
About to cancel forever
Result:  [CancelledError(), 42]
>>> 

MicroPython (behaves as if return_exceptions==False):

MicroPython v1.17 on 2021-09-02; linux version
Use Ctrl-D to exit, Ctrl-E for paste mode
>>> import rats40
forever() will be cancelled
Runs to completion
About to cancel forever
Cancelled
Result:  None
>>> 

Metadata

Metadata

Assignees

No one assigned

    Labels

    extmodRelates to extmod/ directory in source

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0