Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python repeting 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 :: function without return python 
Python :: how to get unique value of all columns in pandas 
Python :: colorbar min max matplotlib 
Python :: python coding questions and answers 
Python :: -1 in numpy reshape 
Python :: convert python datetime to string 
Python :: pymongo [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate 
Python :: python read entire file 
Python :: python read in integers separated by spaces 
Python :: color name to hex python 
Python :: replace string if it contains a substring pandas 
Python :: tkinter entry 
Python :: make a nested list flat python 
Python :: how to disconnect wifi using python 
Python :: copyfile pyhon 
Python :: pandas dataframe 
Python :: last executed query in flask api 
Python :: how to reverse a list in python without using inbuilt function 
Python :: hstack in numpy 
Python :: pvm python 
Python :: python telethon 
Python :: numpy stack arrays vertically 
Python :: python set remove if exists 
Python :: calculate the distance between two points 
Python :: does jupyter notebook need internet 
Python :: python find the average of a list 
Python :: check if two strings are anagrams python 
Python :: python currency format locale 
Python :: dataframe to text file 
Python :: bash python csv to json 
ADD CONTENT
Topic
Content
Source link
Name
6+9 =