Page 1 of 1

2 concurrent while true loops?

Posted: Thu Sep 01, 2022 12:57 pm
by greg_the_aussie
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

Re: 2 concurrent while true loops?

Posted: Thu Sep 01, 2022 1:54 pm
by Roberthh
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.

Re: 2 concurrent while true loops?

Posted: Thu Sep 01, 2022 8:41 pm
by greg_the_aussie
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!

Re: 2 concurrent while true loops?

Posted: Thu Sep 01, 2022 9:02 pm
by davef
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.