Something like Threading/Queue in MicroPython?
-
- Posts: 44
- Joined: Fri Sep 02, 2016 4:07 pm
- Location: Austria
Something like Threading/Queue in MicroPython?
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?
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?
Re: Something like Threading/Queue in MicroPython?
Not as such. However there are alternatives on the ESP32:
- uasyncio, but that might cause you to completely rewrite your scripts
- use lightweight threads with the _thread module. It is documented for the lobo port. However, I cannot find documentation for the mainline (apart form the fact that it is experimental and derived from the CPython module...).
Re: Something like Threading/Queue in MicroPython?
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.
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.
-
- Posts: 44
- Joined: Fri Sep 02, 2016 4:07 pm
- Location: Austria
Re: Something like Threading/Queue in MicroPython?
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...
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...
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: Something like Threading/Queue in MicroPython?
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.
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.
Index to my micropython libraries.
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: Something like Threading/Queue in MicroPython?
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.
Index to my micropython libraries.
-
- Posts: 44
- Joined: Fri Sep 02, 2016 4:07 pm
- Location: Austria
Re: Something like Threading/Queue in MicroPython?
Is there another hardware with faster response? I need MicroPython and 2 hardware serial connections...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.
...
- rcolistete
- Posts: 352
- Joined: Thu Dec 31, 2015 3:12 pm
- Location: Brazil
- Contact:
Re: Something like Threading/Queue in MicroPython?
Pyboard 1.1/D.
My "MicroPython Samples". My "MicroPython Firmwares" with many options (double precision, ulab, etc).
-
- Posts: 5
- Joined: Tue Jan 05, 2021 8:39 am
Re: Something like Threading/Queue in MicroPython?
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.lukesky333 wrote: ↑Wed Mar 13, 2019 8:00 amI want to read data from two serial connections simulatneously.
/Lars