Python: running code every n seconds and restarting the timer provided

It may be easier than I think, but I would like to create a timer that, having reached the limit (say, 15 minutes), executes some code.

Meanwhile, every second I would like to check the condition. If the condition is met, then the timer is reset, and the process starts again, otherwise the countdown continues.

If the condition is satisfied after the end of the countdown, some code is executed, and the timer starts the countdown again.

Does this include threading or can it be achieved with the simple time.sleep() function?

+1
python timer counter
source share
5 answers

If the whole process is as simple as you say, I would do it something like this (semi-psuedo-code):

 def run_every_fifteen_minutes(): pass def should_reset_timer(): pass def main(): timer = 0 while True: time.sleep(1) timer+=1 if should_reset_timer(): timer = 0 if timer == 15*60: run_every_fifteen_minutes() timer = 0 

Please note that this will not be exactly in fifteen minutes. Perhaps it may be too late for a few seconds. Sleep does not guarantee sleep for only 1 second, and the rest of the cycle will take some time. You can add a system time comparison if you need it to be truly accurate.

+2
source share

Maybe you could do it with streaming really elegantly, but if you need a quick fix, you can try

 import time timer = 15 * 60 # 60 seconds times 15 mins while timer > 0: time.sleep(0.985) # don't sleep for a full second or else you'll be off timer -= 1 if someCondition: timer = 15 * 60 executeCode() # called when time is zero and while loop is exited 
+1
source share

The description is like a dead main switch / watchdog timer . How this is implemented depends on your application: is there an event loop, are there blocking functions, do you need a separate process for proper isolation, etc. If no function blocks your code:

 #!/usr/bin/env python3 import time from time import time as timer timeout = 900 # 15 minutes in seconds countdown = timeout # reset the count while True: time.sleep(1 - timer() % 1) # lock with the timer, to avoid drift countdown -= 1 if should_reset_count(): countdown = timeout # reset the count if countdown <= 0: # timeout happened countdown = timeout # reset the count "some code is executed" 

The code assumes that sleep is never interrupted (note: before Python 3.5, sleep can be interrupted by a signal). The code also assumes that no function takes a significant (about a second) time. Otherwise, you must use an explicit deadline (the same code structure):

 deadline = timer() + timeout # reset while True: time.sleep(1 - timer() % 1) # lock with the timer, to avoid drift if should_reset_count(): deadline = timer() + timeout # reset if deadline < timer(): # timeout deadline = timer() + timeout # reset "some code is executed" 
  • What is the best way to execute a function multiple times every x seconds in Python?
  • How to run a function periodically in python
+1
source share

You might want to look into the cron tool for Linux to schedule your script to run.

0
source share

Thanks for helping everyone, your answers pointed me in the right direction. In the end, I came up with:

 #!/usr/bin/python import RPi.GPIO as GPIO import time import subprocess GPIO.setmode(GPIO.BCM) PIR_PIN = 4 GPIO.setup(PIR_PIN, GPIO.IN) timer = 15 * 60 # 60 seconds times 15 mins subprocess.call("sudo /opt/vc/bin/tvservice -o", shell=True) try : print "Screen Timer (CTRL+C to exit)" time.sleep(5) print "Ready..." while True: time.sleep(0.985) # Test PIR_PIN condition current_state = GPIO.input(PIR_PIN) if timer > 0: timer -= 1 if current_state: #is true # Reset timer timer = 15 * 60 else: if current_state: #is true subprocess.call("sudo /opt/vc/bin/tvservice -p", shell=True) # Reset timer timer = 15 * 60 else: subprocess.call("sudo /opt/vc/bin/tvservice -o", shell=True) except KeyboardInterrupt: print "Quit" GPIO.cleanup() 

To express this in context, I use a PIR sensor to detect movement and turn on a connected hdmi monitor on a raspberry Pi. After 15 minutes without movement, I want to turn off the monitor, and then if (at a later time) movement is detected, turn it on again and restart the time.

0
source share

All Articles