thread vs uasyncio

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
Pauline_Carlson
Posts: 1
Joined: Wed Jan 26, 2022 3:32 pm
Contact:

thread vs uasyncio

Post by Pauline_Carlson » Wed Jan 26, 2022 3:41 pm

Hello there,

My project needs multithreading and some kind of locks.
As far as I know, _thread has not supported any kind of locks.
So do you know a workaround for that?
async could be one, but it is not supported on pycom boards. Is there a way to use it?
Which path should I choose?

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: thread vs uasyncio

Post by pythoncoder » Wed Jan 26, 2022 6:51 pm

The _thread module is experimental and poorly documented. However you should be able to issue

Code: Select all

lock = _thread.allocate_lock()
lock.acquire()
# ...
lock.release()
That said, uasyncio is the best solution in most cases: it is very much more efficient on lightweight targets. Developing under cooperative schedulers is much easier than with preemptive ones. I can point you at docs to support this assertion if you wish.

If Pycom don't support asynchronous programming, their firmware is ill suited for serious applications (in my opinion).
Peter Hinch
Index to my micropython libraries.

Post Reply