Something like Threading/Queue in MicroPython?

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
lukesky333
Posts: 44
Joined: Fri Sep 02, 2016 4:07 pm
Location: Austria

Something like Threading/Queue in MicroPython?

Post by lukesky333 » Tue Mar 12, 2019 8:19 am

Hi guys,

I've a script at my desktop and I'll try to port it to MicroPython (esp. ESP32). Therefore I need libraries like "threading" and "Queue".
Is there something similar in MicroPython?

fstengel
Posts: 55
Joined: Tue Apr 17, 2018 4:37 pm

Re: Something like Threading/Queue in MicroPython?

Post by fstengel » Tue Mar 12, 2019 8:34 am

Not as such. However there are alternatives on the ESP32:

User avatar
mattyt
Posts: 410
Joined: Mon Jan 23, 2017 6:39 am

Re: Something like Threading/Queue in MicroPython?

Post by mattyt » Tue Mar 12, 2019 8:55 am

Without knowing your use case, it's hard to say what the right solution is - but asyncio is a fair bet. ;) Peter Hinch has implemented Queue for asyncio. Check it out and see if it fits.

Threads are possible but they come with added complexity and a less mature implementation (no threading library only the _thread primitives) and documentation. If you do want to pursue this approach then Damien's talk on _thread for the ESP32 on MicroPython may be useful.

lukesky333
Posts: 44
Joined: Fri Sep 02, 2016 4:07 pm
Location: Austria

Re: Something like Threading/Queue in MicroPython?

Post by lukesky333 » Wed Mar 13, 2019 8:00 am

I want to read data from two serial connections simulatneously. The challenge is to get the data immediately, without delay - to get the correct "timestamp". Therefore I use threading and under Ubuntu it is working fine.

Later I put the data in the queue to handle it...

Now I want to port this to ESP32 and I'm looking for a solution/alternative for Queue and threading...

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

Re: Something like Threading/Queue in MicroPython?

Post by pythoncoder » Wed Mar 13, 2019 8:33 am

I suggest you quantify "simultaneously". Any scheduler, pre-emptive or cooperative, introduces latency. The amount depends on the target. For absolute minimum latency you'd need to handle the UART IRQ's directly in code. Even that isn't a panacea: ESPx chips aren't known for fast response to IRQ's.

If you can tolerate latency in the low tens of ms I would suggest using the uasyncio stream mechanism rather than queues. My uasyncio fork suits exactly this type of application by minimising latency.

Whether _thread offers any latency benefit on ESP32 is moot and depends on the characteristics of the underlying vendor code. You'd need to measure the two approaches to determine which was best.
Peter Hinch
Index to my micropython libraries.

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

Re: Something like Threading/Queue in MicroPython?

Post by pythoncoder » Wed Mar 13, 2019 8:37 am

mattyt wrote:
Tue Mar 12, 2019 8:55 am
...Peter Hinch has implemented Queue for asyncio...
To prevent misunderstanding uasyncio.queues is a part of uasyncio and was written by Paul Sokolovsky. I merely documented its use.
Peter Hinch
Index to my micropython libraries.

lukesky333
Posts: 44
Joined: Fri Sep 02, 2016 4:07 pm
Location: Austria

Re: Something like Threading/Queue in MicroPython?

Post by lukesky333 » Wed Mar 13, 2019 10:07 am

pythoncoder wrote:
Wed Mar 13, 2019 8:33 am
...
Even that isn't a panacea: ESPx chips aren't known for fast response to IRQ's.
...
Is there another hardware with faster response? I need MicroPython and 2 hardware serial connections...

User avatar
rcolistete
Posts: 352
Joined: Thu Dec 31, 2015 3:12 pm
Location: Brazil
Contact:

Re: Something like Threading/Queue in MicroPython?

Post by rcolistete » Thu Mar 14, 2019 12:34 am

Pyboard 1.1/D.
My "MicroPython Samples". My "MicroPython Firmwares" with many options (double precision, ulab, etc).

lrb@lrb.no
Posts: 5
Joined: Tue Jan 05, 2021 8:39 am

Re: Something like Threading/Queue in MicroPython?

Post by lrb@lrb.no » Wed Jan 20, 2021 10:41 am

lukesky333 wrote:
Wed Mar 13, 2019 8:00 am
I want to read data from two serial connections simulatneously.
Did you solve this? I'm looking into a similar task: reading and writing four serial ports, having a main handler that responds to received data and decides where to send the data.
/Lars

Post Reply