Timer that breaks when any input is given in Python
Question:
I am trying to find a way to create a timer in python that keeps counting while the user doesn’t input anything. The goal is to then create a function that automatically backs up the data from the session to a separate JSON file if the time of inactivity exceeds 10 mins. If I start an infinite loop that breaks when there is an input it will stop the rest of the code from executing. Also using time.time()
in the traditional way to measure execution time (subtract end from start) isn’t useful here because I don’t want to measure the execution time after a particular line. How could I go about this? Thanks!
Bellow is a simplified format of the block I wish to apply this to.
def run(self):
run = True
while run:
command_input = input("Please type your command"):
# some function that either breaks when user inputs or starts series of action if time exceeds 10 mins
if command == "a":
function_one()
else:
function_two()
EDIT – SOLUTION IN MY SCRIPT’S CONTEXT
class Session:
def __init__(self):
self.last_command_timing = time.time()
def run(self):
run = True
threading.Thread(target=self.check_time_limit).start()
while run:
command_input = input("Please type your command"):
if command == "a":
self.last_command_timing = time.time()
function_one()
else:
self.last_command_timing = time.time()
function_two()
def check_time_limit(self):
while True:
if time.time() - self.last_command_timing> 600 :
backup_function()
break
def backup_function(self):
# some backing up func
Answers:
The input() function in python blocks the thread. So if you want to use input and save something simultaneously, you have to create another thread.
import threading
import time
lastActive = time.time()
# function which is running in the background and saves the data if needed
def save():
while True:
if time.time() - lastActive > 600:
# Save ...
# This call starts a new thread
threading.Thread(target=save).start()
# The main while loop
while True:
command = input("Please type your command")
lastActive = time.time()
# Process the command ...
This might be helpful. Good luck!
https://pypi.org/project/pytimedinput/
Edit: including example, as per request.
First install: pip install pytimedinput
Example:
from pytimedinput import timedInput
userText, timedOut = timedInput("Please, do enter something: ")
if(timedOut):
print("Timed out when waiting for input.")
print(f"User-input so far: '{userText}'")
else:
print(f"User-input: '{userText}'")
I am trying to find a way to create a timer in python that keeps counting while the user doesn’t input anything. The goal is to then create a function that automatically backs up the data from the session to a separate JSON file if the time of inactivity exceeds 10 mins. If I start an infinite loop that breaks when there is an input it will stop the rest of the code from executing. Also using time.time()
in the traditional way to measure execution time (subtract end from start) isn’t useful here because I don’t want to measure the execution time after a particular line. How could I go about this? Thanks!
Bellow is a simplified format of the block I wish to apply this to.
def run(self):
run = True
while run:
command_input = input("Please type your command"):
# some function that either breaks when user inputs or starts series of action if time exceeds 10 mins
if command == "a":
function_one()
else:
function_two()
EDIT – SOLUTION IN MY SCRIPT’S CONTEXT
class Session:
def __init__(self):
self.last_command_timing = time.time()
def run(self):
run = True
threading.Thread(target=self.check_time_limit).start()
while run:
command_input = input("Please type your command"):
if command == "a":
self.last_command_timing = time.time()
function_one()
else:
self.last_command_timing = time.time()
function_two()
def check_time_limit(self):
while True:
if time.time() - self.last_command_timing> 600 :
backup_function()
break
def backup_function(self):
# some backing up func
The input() function in python blocks the thread. So if you want to use input and save something simultaneously, you have to create another thread.
import threading
import time
lastActive = time.time()
# function which is running in the background and saves the data if needed
def save():
while True:
if time.time() - lastActive > 600:
# Save ...
# This call starts a new thread
threading.Thread(target=save).start()
# The main while loop
while True:
command = input("Please type your command")
lastActive = time.time()
# Process the command ...
This might be helpful. Good luck!
https://pypi.org/project/pytimedinput/
Edit: including example, as per request.
First install: pip install pytimedinput
Example:
from pytimedinput import timedInput
userText, timedOut = timedInput("Please, do enter something: ")
if(timedOut):
print("Timed out when waiting for input.")
print(f"User-input so far: '{userText}'")
else:
print(f"User-input: '{userText}'")