asyncio on a second thread #12423
-
Hi all, |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 3 replies
-
Currently in micropython there's only one global asyncio event loop, I don't think it will be possible to have a separate loop on a second thread. |
Beta Was this translation helpful? Give feedback.
-
@bjpirt This discussion was about running There can only be one instance of |
Beta Was this translation helpful? Give feedback.
-
Thanks for the replies all - so I should be good to run a single instance of uasyncio on Core 1, but it's better to use Core 0 if I need to access hardware. |
Beta Was this translation helpful? Give feedback.
-
There's also some previous discussion about separate thread loops here: #8340 |
Beta Was this translation helpful? Give feedback.
@bjpirt As long as you only run a single event loop, there's no problem. (@andrewleech -- asyncio doesn't know about threads and doesn't have any thread-local storage, it doesn't care which thread it's running on).
That said, you need to be very careful that you do not access/modify any asyncio internal state concurrently (e.g. from another thread or interrupt handler). e.g. you may not call
create_task
from other threads, orEvent.set
, while asyncio is running (on any thread). MicroPython has asyncio.ThreadSafeFlag which you can use to safely signal into asyncio from another thread or interrupt handler.