2 concurrent while true loops?

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
greg_the_aussie
Posts: 8
Joined: Wed Jul 03, 2019 8:32 pm

2 concurrent while true loops?

Post by greg_the_aussie » Thu Sep 01, 2022 12:57 pm

Hello folks.
I’ve been trying to get some scripts to work together unsuccessfully for the past few nights. Nothing complicated. Just ideas I have put together from sources like this one.
Boot with Access Point on
Main runs a loop that takes a measurement from a sensor and writes it to reading.txt once each day, then main imports server
Server.py is a socket server with a while true loop so I can connect to the board and download reading.txt.
Is it possible that my frustration is because the board cannot handle two while true loops at once? I hope so because then I can look for a different way to support my plan. Otherwise I will keep looking for script errors.
Grateful for your insights.
Greg

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: 2 concurrent while true loops?

Post by Roberthh » Thu Sep 01, 2022 1:54 pm

Basically the board runs a single task, and boot.py and main.py run sequentially.
To get more that a single task handled, you have at least two options:

1. Threading. That allows to start additional tasks which run in parallel to the first task. Therading uses exhausting multiprocessing.
2. Aysncio, which is cooperative multitasking.

In any way, multitasking requires to arrange for concurrent access to single ressources, like a file or an interface.
If the demand is simple, you may also get along with a single while loop, which does both.

greg_the_aussie
Posts: 8
Joined: Wed Jul 03, 2019 8:32 pm

Re: 2 concurrent while true loops?

Post by greg_the_aussie » Thu Sep 01, 2022 8:41 pm

Thank you very much Robert. You’ve set my mind at ease and thank you also for the tips for where to look next for a way forward.
You are a beautiful human being!

davef
Posts: 811
Joined: Thu Apr 30, 2020 1:03 am
Location: Christchurch, NZ

Re: 2 concurrent while true loops?

Post by davef » Thu Sep 01, 2022 9:02 pm

Is this so that you can access the board at any time to get information from it?

One of my control loops the remote every 60 seconds connects to the network, says that it is listening for any requests from the control unit for 5 seconds and then disconnects. This uses sockets. I didn't want to be connected all the time.

Another system uses ESPNow/WiFi as per glenn20 Github and in one main loop checks to see if there are any remote messages about once a second. The messages from the remote appear to get saved in a buffer, which you can process later.

I did wonder if the same mechanism is available in normal WiFi sockets but couldn't work that out.

Post Reply