-
-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Closed
Labels
py-coreRelates to py/ directory in sourceRelates to py/ directory in source
Description
My code heavily relies on coroutines which get canceled quite often.
Occasionally it happens, that a coroutine gets put on the loop but then canceled before it gets executed for the first time.
Apparently this is not possible as it throws a TypeError:
import uasyncio as asyncio
>>> async def test():
... print("test")
>>> t=test()
>>> async def cancel():
... asyncio.cancel(t)
>>> loop=asyncio.get_event_loop()
>>> loop.create_task(cancel())
>>> loop.create_task(t)
>>> loop.run_forever()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "uasyncio/core.py", line 109, in run_forever
File "<stdin>", line 2, in cancel
File "uasyncio/core.py", line 273, in cancel
TypeError: can't pend throw to just-started generator
This is incompatible to CPython and causes some problems for me because suddenly there is no way to cancel this coroutine unless I wait until it yields for the first time.
An example of where this might happen is if an asyncio task is created when an MQTT connection is established and the same task gets cancelled when the connection breaks. So if the connection breaks until the task gets executed, it would run into this error.
Is this fixable or just the way generators work in micropython?
Metadata
Metadata
Assignees
Labels
py-coreRelates to py/ directory in sourceRelates to py/ directory in source