Python how can I manually end an infinite while loop that's collecting data, without ending the code and not using KeyboardInterrupt?

In my code I have a “while True:” loop that needs to run for a varying amount of time while collecting live data (3-5 hours). Since the time is not predetermined I need to manually end the while loop without terminating the script, so that it may continue to the next body of code in the script.

I do not want to use “input()” at the end of the loop, because then I have to manually tell it to continue looping every time it finishes the loop, I am collecting live data down to the half second, so this is not practical.

Also I do not want to use keyboard interrupt, have had issues with it. Are there any other solutions? All I have seen is try/except with “keyboardinterrupt”

def datacollect()
def datacypher()

while True:
    #Insert code that collects data here
    datacollect()

#end the while loop and continue on
#this is where i need help

datacypher()
print('Yay it worked, thanks for the help')

I expect to end the loop manually and then continue onto the code that acts upon the collected data.

If you need more details or have problem with my wording, let me know. I have only asked one question before. I am learning.

Solution:

How about adding a key listener in a second thread? After you press Enter, you’ll manually move the script to the next stage by means of a shared bool. The second thread shouldn’t slow down the process since it blocks on input().

from threading import Thread
from time import sleep

done = False

def listen_for_enter_key_press():
    global done
    input()
    done = True

listener = Thread(target=listen_for_enter_key_press)
listener.start()

while not done:
    print('working..')
    sleep(1)

listener.join()

print('Yay it worked, thanks for the help')

Syntax for a single-line Bash while loop with condition

I gone through single line inifinite while loop in bash, and trying to add a single line while loop with condition. Below I have mentioned my command, which gives unexpected result (It is suppose to stop after 2 iterations, but it never stops. And also it considers variable I as executable).

Command:

i=0; while [ $i -lt 2 ]; do echo "hi"; sleep 1; i = $i + 1; done

Output:

hi
The program 'i' is currently not installed. To run 'i' please ....
hi 
The program 'i' is currently not installed. To run 'i' please ....
hi 
The program 'i' is currently not installed. To run 'i' please ....
...
...

Note: I am running it on Ubuntu 14.04

Solution:

bash is particular about spaces in variable assignments. The shell has interpreted i = $i + 1 as a command i and the rest of them as arguments to i, that is why you see the errors is saying i is not installed.

In bash just use the arithmetic operator (See Arithmetic Expression),

i=0; while [ $i -lt 2 ]; do echo "hi"; sleep 1; ((i++)); done

You could use the arithmetic expression in the loop context also

while((i++ < 2)); do echo hi; sleep 1; done

POSIX-ly

i=0; while [ $i -lt 2 ]; do echo "hi"; sleep 1; i=$((i+1)); done

POSIX shell supports $(( )) to be used in a math context, meaning a context where the syntax and semantics of C’s integer arithmetic are used.