Threading on PyBoard 1.1

The official pyboard running MicroPython.
This is the reference design and main target board for MicroPython.
You can buy one at the store.
Target audience: Users with a pyboard.
Post Reply
lrb@lrb.no
Posts: 5
Joined: Tue Jan 05, 2021 8:39 am

Threading on PyBoard 1.1

Post by lrb@lrb.no » Tue Jan 19, 2021 12:03 pm

Hi,
I have a PyBoard 1.1 and I plan to use this with a multithreading program
The program has been developed on a Rpi and is working well on rpi.
My PyBoard: MicroPython v1.13 on 2020-09-02; PYBv1.1 with STM32F405RG

I see there are one firmware download marked "threading: v1.13-274-g49dd9ba1a (latest)".
Should I update my PyBoard?

Thanks,
Lars

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Threading on PyBoard 1.1

Post by jimmo » Tue Jan 19, 2021 10:59 pm

lrb@lrb.no wrote:
Tue Jan 19, 2021 12:03 pm
I see there are one firmware download marked "threading: v1.13-274-g49dd9ba1a (latest)".
Should I update my PyBoard?
The threading build is identical to the normal build other than it enables the _thread module and adds some extra locking in a few places.

On MicroPython (and regular Python TBH) I'd strongly recommend using asyncio instead of threads though.

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

Re: Threading on PyBoard 1.1

Post by lrb@lrb.no » Wed Jan 20, 2021 9:55 am

Thank you Jimmo!
I'll look into that.
The reasons for using multiprocessing in my original program for RPI was the possibility to use Queues.
If using uasyncio, can you give a hint how to implement something similar?
/Lars

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

Re: Threading on PyBoard 1.1

Post by pythoncoder » Wed Jan 20, 2021 10:21 am

CPython's asyncio has a Queue class. There is not yet an official implementation, but an unofficial one may be found here.
Peter Hinch
Index to my micropython libraries.

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

Re: Threading on PyBoard 1.1

Post by lrb@lrb.no » Thu Jan 21, 2021 11:17 am

Thank you pythoncoder!
I've done a simple test of the Queue implementation and it seems to be working very well!

A question, might be a little off topic:
Currently I have four uart with external electronics for RS232, and I need to read and write to all.

In my python program for rpi I used two classes: One for read and one for write, each having their own queue.
Would you recommend using classes on Pyboard 1.1 as well? Or should I use functions? I.E. 8 functions..

Kind regards,
Lars

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

Re: Threading on PyBoard 1.1

Post by pythoncoder » Thu Jan 21, 2021 4:23 pm

If you're using uasyncio - which is usually the best approach - you should use the stream mechanism which handles concurrency and buffering.
Peter Hinch
Index to my micropython libraries.

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

Re: Threading on PyBoard 1.1

Post by lrb@lrb.no » Thu Jan 21, 2021 4:32 pm

Thank you Peter,
So 8 functions with stream mechanism is your recommendation.
I'll give it a try :)
/Lars

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

Re: Threading on PyBoard 1.1

Post by pythoncoder » Thu Jan 21, 2021 4:38 pm

You will need eight asynchronous routines. Whether these are functions or bound methods depends on the way you plan to share data. This is down to general programming practice - bound variables or bound objects are generally better practice than using globals.
Peter Hinch
Index to my micropython libraries.

Post Reply