Replies: 3 comments 2 replies
-
@bixb922 I think you are right about #8929, I confirm this happens with a pyboard too ...
await task() or if you create a task with ret = await uasyncio.gather(task) Here is my working code import sys
import uasyncio
print(sys.implementation)
async def task():
await uasyncio.sleep(10)
raise ValueError("wrong value")
print("hello")
return 0
async def main():
task1 = uasyncio.create_task(task())
await uasyncio.sleep(2)
print("Waiting...")
try:
ret = await uasyncio.gather(task1)
print(ret)
except Exception as e:
print(e)
uasyncio.run(main())
|
Beta Was this translation helpful? Give feedback.
-
Yes, I think the task must be still running to be gathered?, that's why I increased sleep time, otherwise I got the same error.
I've no idea how this compares to CPython, but yes I think it worth exploring and I think this tutorial is the goto place for uasyncio MicroPython peterhinch/micropython-async |
Beta Was this translation helpful? Give feedback.
-
It is possible to create a global exception handler which traps untrapped exceptions: import uasyncio as asyncio
import sys
def _handle_exception(loop, context):
print('Global handler')
sys.print_exception(context["exception"])
#loop.stop()
sys.exit() # Drastic - loop.stop() does not work when used this way
async def bar():
await asyncio.sleep(0)
1/0 # Crash
async def main():
loop = asyncio.get_event_loop()
loop.set_exception_handler(_handle_exception)
asyncio.create_task(bar())
for _ in range(5):
print('Working')
await asyncio.sleep(0.5)
asyncio.run(main()) This is discussed here |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I use asyncio a lot (it makes life easy, especially when I think back to coding event loops in assembly language in the 1970ies ;-)
What I was trying to do is to await end task that had been cancelled, but an exception occurred in the task after cancelling it. The microcontroller reboots always. Issue #8929 seems to be related.
The microcontroller is a ESP32-WROOM-32D packaged on ESP32-DEVKITC-V4. I also tried with a ESP32-S3 with the same version 1.19.1, same result. I attached a test program and the output.
Please tell me if I am doing something wrong. Code and output follows:
The output is as follows:
Beta Was this translation helpful? Give feedback.
All reactions