rum404
rum404

Reputation: 97

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.

Upvotes: 5

Views: 856

Answers (3)

ggorlen
ggorlen

Reputation: 57195

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')

Upvotes: 5

chepner
chepner

Reputation: 532093

One way to interrupt the loop is to use signals.

import signal

def handler(signum, stackframe):
    global DONE
    DONE = True

signal.signal(signal.SIGUSR1, handler)

DONE = False
while not DONE:
    datacollect()

datacypher()

The loop will continue until your program receives the USR1 signal (sent from the shell, for example, by kill -s USR1 <pid>, where <pid> is your program's process ID), at which point DONE will be True the next time your loop tests its value.

You can adapt this for keyboard interrupts simply by installing handler as the handler for signal.SIGINT instead of signal.SIGUSR1, since the default signal handler is what raises a KeyboardInterrupt exception in the first place.

Upvotes: 3

brechmos
brechmos

Reputation: 1316

One option, is you could look for the existence of a file, e.g.:

import os.path

fname = '/tmp/stop_loop'

def datacollect()
def datacypher()

while not os.path.isfile(fname):
    #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')

If that file does not exist it will continue to go through the while loop. Then, when you want to stop the while loop you can just do touch /tmp/stop_loop and the while loop will stop.

I suspect the isfile() should be a reasonably efficient, so maybe this would not be too bad.

Upvotes: 0

Related Questions