Page 1 of 1

Threading on PyBoard 1.1

Posted: Tue Jan 19, 2021 12:03 pm
by lrb@lrb.no
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

Re: Threading on PyBoard 1.1

Posted: Tue Jan 19, 2021 10:59 pm
by jimmo
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.

Re: Threading on PyBoard 1.1

Posted: Wed Jan 20, 2021 9:55 am
by lrb@lrb.no
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

Re: Threading on PyBoard 1.1

Posted: Wed Jan 20, 2021 10:21 am
by pythoncoder
CPython's asyncio has a Queue class. There is not yet an official implementation, but an unofficial one may be found here.

Re: Threading on PyBoard 1.1

Posted: Thu Jan 21, 2021 11:17 am
by lrb@lrb.no
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

Re: Threading on PyBoard 1.1

Posted: Thu Jan 21, 2021 4:23 pm
by pythoncoder
If you're using uasyncio - which is usually the best approach - you should use the stream mechanism which handles concurrency and buffering.

Re: Threading on PyBoard 1.1

Posted: Thu Jan 21, 2021 4:32 pm
by lrb@lrb.no
Thank you Peter,
So 8 functions with stream mechanism is your recommendation.
I'll give it a try :)
/Lars

Re: Threading on PyBoard 1.1

Posted: Thu Jan 21, 2021 4:38 pm
by pythoncoder
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.