Zachgober
Posts: 6
Joined: Thu Jun 06, 2019 10:10 am

Running multiple while true loops

Thu Jun 06, 2019 10:14 am

I am using a pi to run a relay board for a pump timer. Also I want to be able to output a ds18b20 temp sensor for water temp. Is it possible to run two while true loops in the same program. If so how do I write the code to be able to control more than one function in a program.

pcmanbob
Posts: 6939
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Running multiple while true loops

Thu Jun 06, 2019 11:28 am

Hi.

Simple answer is NO you can't.

because once you enter the first while true loop you will never leave it , unless you specifically program it to break from the loop and enter the second while true loop but then you have the same problem and continually breaking out of a while true loop is not a good way to program.

if you want to do more than one thing then you simply need to have all the things you want to do running in the one while true loop.

or have loops that execute for a number of times then exit and move on to the next loop which does the same , but you would still need an over all while true loop to keep the program running.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

User avatar
thagrol
Posts: 1849
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: Running multiple while true loops

Thu Jun 06, 2019 2:14 pm

More complex answer: Use threads. One while loop per thread.

Hit the python docs and your preferred search engine for more info on threads in python.
This space unintentionally left blank.

pfletch101
Posts: 510
Joined: Sat Feb 24, 2018 4:09 am
Location: Illinois, USA

Re: Running multiple while true loops

Fri Jun 07, 2019 3:36 pm

thagrol wrote:
Thu Jun 06, 2019 2:14 pm
More complex answer: Use threads. One while loop per thread.

Hit the python docs and your preferred search engine for more info on threads in python.
Slightly more complex answer, still: use multiprocessing. The problem with threads in Python is that a single process can only have one thread active at a time. If you have multiple threads that need to wait for external events (including sleep()), threading will usually work. If you have one or more 'tight' while True loops (not usually a good idea, for many reasons), threading will probably not work well for you.

ghp
Posts: 1411
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: Running multiple while true loops

Fri Jun 07, 2019 6:42 pm

Just to increase complexity: modern concepts are asynchronous. See https://docs.python.org/3/library/asyncio.html

pfletch101
Posts: 510
Joined: Sat Feb 24, 2018 4:09 am
Location: Illinois, USA

Re: Running multiple while true loops

Fri Jun 07, 2019 8:50 pm

ghp wrote:
Fri Jun 07, 2019 6:42 pm
Just to increase complexity: modern concepts are asynchronous. See https://docs.python.org/3/library/asyncio.html
A quick scan did not tell me what asyncio was doing 'under the hood', but it is presumably either using threads or subprocesses (or both) and providing a 'wrapper' round them. As I noted, for tasks that are i/o-bound, threads should work fine, so they may well be what is being used.

Return to “Python”