Skip to content

uasyncio: current_task behaviour differs from CPython #11530

Closed

Description

@peterhinch

Background: I am looking for a way detect whether a function has been called in a uasyncio context. The asyncio_running function works in CPython, but in MP it fails if a uasyncio script has previously run. In the absence of a fix, a non-hacky workround would be appreciated :)

Repro:

try:
    import uasyncio as asyncio
except:
    import asyncio

def asyncio_running():
    try:
        _ = asyncio.current_task()
    except:
        return False
    return True

def prun(txt):
    print(f"{txt}: asyncio {'' if asyncio_running() else 'not'} running")

prun("Start")

async def foo():
    prun("In task")
    await asyncio.sleep(1)

try:
    asyncio.run(foo())
finally:
    _ = asyncio.new_event_loop()

prun("At end")

On CPython 3.8.10:

>>> import rats25
Start: asyncio not running
In task: asyncio  running
At end: asyncio not running

On MP 1.20 (tested on Unix and RP2):

>>> import rats25
Start: asyncio not running
In task: asyncio  running
At end: asyncio  running

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions