Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python recurrent timer

from threading import Timer
import time

class RepeatTimer(Timer): # Class to have an ever-repeating timer
    daemon=True # So that the thread stops if there is a keyboard interrupt
    def run(self):
        while not self.finished.wait(self.interval):
            self.function(*self.args, **self.kwargs)

def dummyfn(msg="foo"): # Function that executes whenever the timer is called
    print(msg)

timer = RepeatTimer(1, dummyfn) # Timer executing the previous function every second
timer.start() # Starting the timer
while 1: # Rest of the code
    print("Hello")
    time.sleep(0.5)
timer.cancel() # If you want to stop the timer

# This should execute the main program (print "Hello" every 0.5 seconds), and execute
# the timer function every 1 s, no matter what happens in the main program
Comment

PREVIOUS NEXT
Code Example
Python :: python add up values in list 
Python :: multiply each element in list python 
Python :: turn df to dict 
Python :: how to calculate sum of a list in python 
Python :: how to convert fahrenheit to celsius in python 
Python :: concat dataframes 
Python :: how to download instagram profile picture with the help of python 
Python :: python opencv imresize 
Python :: como leer lineas de un archivo de texto en python 
Python :: decimal in python 
Python :: python plot multiple lines in same figure 
Python :: python classes 
Python :: append path to sys jupyter notebook 
Python :: pytest multi thread 
Python :: number of words in a string python 
Python :: dataframe create 
Python :: scroll down selenium python 
Python :: csv writer python 
Python :: how to url encode using python django 
Python :: how to custom page not found in django 
Python :: youtube-dl python get file name 
Python :: Import "whitenoise.django" could not be resolved 
Python :: check dir exist python 
Python :: sympy function definition 
Python :: discord.py how to print audit logs 
Python :: python average of list 
Python :: isolate row based on index pandas 
Python :: discord.py read embed on message 
Python :: saving model in pytorch 
Python :: networkx max degree node 
ADD CONTENT
Topic
Content
Source link
Name
7+4 =