QUERY · ISSUE
uasyncio: Behaviour differs from CPython.
bug
Consider this script:
try:
import uasyncio as asyncio
except ImportError:
import asyncio
evt = asyncio.Event()
async def foo():
try:
await evt.wait()
finally:
print('Finally')
async def main():
t = asyncio.create_task(foo())
await asyncio.sleep(1)
t. cancel()
# Can be fixed with .sleep(0) here
print('canned')
await asyncio.sleep(1)
asyncio.run(main())
On MP the outcome is:
MicroPython v1.19.1-617-g43dd3ea74 on 2022-11-08; linux [GCC 9.4.0] version
Use Ctrl-D to exit, Ctrl-E for paste mode
>>> import rats
canned
Finally
>>>
On CPython:
$ python3
Python 3.8.10 (default, Jun 22 2022, 20:18:18)
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import rats
Finally
canned
>>>
MP behaviour also confirmed on Pyboard.
CANDIDATE · ISSUE
uasyncio: current_task behaviour differs from CPython
bug
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