Multitasking?

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
JumpZero
Posts: 54
Joined: Mon Oct 30, 2017 5:54 am
Location: Arcachon - France

Multitasking?

Post by JumpZero » Thu Nov 02, 2017 9:07 am

Hi!

I always write my code in this way: doing everything from timers.
I mean my main.py is launched at boot initializes everything it has to, launches a few timers that will do periodic tasks (e.g start the pump to water the plants) and then terminates.
I find very convenient to do so because I do not have a program running continuously in a loop and therefore I can communicate with the system thru webrepl.
I communicate with the system and at the same time the timers will launch the tasks they have to. It works well even with timers doing tasks very often (e.g every seconds).
So it’s a king of multitasking...
Is there anything wrong with that?

Thks
--
Jmp0

mwm
Posts: 36
Joined: Wed Aug 09, 2017 3:52 pm

Re: Multitasking?

Post by mwm » Fri Nov 03, 2017 6:00 pm

Depends. How are your modules communicating with each other? Are you using some kind of synchronization object, or just passing around Python objects?

In general, most Python modules haven't been vetted for thread safety, so running in this kind of multi-threaded environment is a bit hazardous. The few I know of that have been vetted aren't safe. Between the GIL and the interpreter forcing threads into a sort of cooperative multi-tasking, the cPython is safer than I'd assume.

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

Re: Multitasking?

Post by pythoncoder » Sat Nov 04, 2017 7:04 am

@mwm I agree - that kind of coding needs a lot of care. The lack of thread-safety can be addressed by using mutexes and by disabling interrupts in critical sections but there is plenty of scope for rarely occurring, hard to locate, bugs.

@JumpZero I recommend using cooperative multi-tasking unless there is an inescapable reason for needing a pre-emptive model. This paradigm requires you to determine the points in the code where other tasks can run and hence greatly reduces the potential for nasty bugs. See the uasyncio module.
Peter Hinch
Index to my micropython libraries.

JumpZero
Posts: 54
Joined: Mon Oct 30, 2017 5:54 am
Location: Arcachon - France

Re: Multitasking?

Post by JumpZero » Fri Nov 10, 2017 7:34 am

Thanks for your advices, it's helpful.
I'll consider that and be careful.
--
Jmp0

Post Reply