Strange behavior when using _thread and asyncio on Raspberry Pi Pico #16899
-
Board: W5500 EVB PICO I've been playing around a bit with _thread and asyncio, trying to combine them. I'd like to have one task running on each processor. Doing that, I got the following code import asyncio, time, _thread
async def c0():
while True:
print(0)
time.sleep(0.1)
async def c1():
while True:
print(1)
time.sleep(0.1)
async def app0():
asyncio.create_task(c0())
await asyncio.sleep(1000)
async def app1():
asyncio.create_task(c1())
await asyncio.sleep(1000)
print('test')
_thread.start_new_thread(lambda: asyncio.run(app1()), ())
asyncio.run(app0())
while True: pass I have been able to produce the following outputs/errors while running the same code.
^-- that one worked just fine
^-- this one just died? Keyboard interrupts did nothing
^-- this one is from a slightly older version of my test script (sadly I didn't save that one). But IF I remember correctly, the only things I've changed where adding the I've used Thonny and the VS Code Micropython extension for running the code. Somethings seems off here. Am I not supposed to use asyncio on the core1? I am new to python multitasking, so maybe one isn't supposed to do that? But even then, a more consistent error would be nice. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
Trying to recreate the heap error, I've been able to generate a view more outputs/error messages using import asyncio, time, _thread
async def c0():
while True:
print(0)
time.sleep(0.1)
async def c1():
while True:
print(1)
time.sleep(0.1)
async def app0():
asyncio.create_task(c0())
asyncio.create_task(c1())
await asyncio.sleep(1000)
async def app1():
asyncio.create_task(c1())
asyncio.create_task(c0())
await asyncio.sleep(1000)
print('test')
_thread.start_new_thread(lambda: asyncio.run(app1()), ())
asyncio.run(app0())
while True: pass
^-- I've just rerun the script, without hard reseting the Pico. After printing test, it didn't respond. Funnily enough, when running this one in Thonny, only one core even prints stuff:
So it looks like, the editors may play a part in this. I'd like to know if the same issue happens, when running this directly/as frozen byte-code, but I haven't tried that jet. |
Beta Was this translation helpful? Give feedback.
-
You cannot run |
Beta Was this translation helpful? Give feedback.
I've only ever used
asyncio
on core 0. I would expect it to work on core 1, but see below.In general MicroPython support for threading is rudimentary - see the warnings in the official docs. Most MicroPython objects are not thread-safe. Unless docs specifically state that an object is thread-safe, assume it is not. For technical details on the problems with sharing objects between threads, see this doc.
My advice on using the second core is to avoid it if possible:
asyncio
is all you need for most concurrency problems. If you are sure that a given problem cannot be solved by means ofasyncio
alone, carefully minimise the use of threading, read the above doc, and hold on tight for a chall…