How Python implements the timer function

Timer: Call a function every certain time. If you want to call a function every certain period of time, you must set the Timer again in the function called by the Timer. Timer is a derived class of Thread

Threads in python provide a subset of java thread functions.

#! /usr/bin/env python
from threading import Timer
import time
timer_interval=1
def delayrun():
 print 'running'
t=Timer(timer_interval,delayrun)
t.start()while True:
 time.sleep(0.1)
 print 'main running'

t is a Timer object. Execute the delayrun function after delay one second.

The time.sleep function is used to let the main thread pause for a while before continuing.

Example extension:

Python3 timer task code

import time
import sys
import signal
import datetime
import threading
# Timer
def schedule_update():
t = threading.Timer(0, event_func)
t.setDaemon(True)
t.start()
# Execution function
def event_func():
now_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')print(now_time)exec_update()
# update_openvas_dbs_from_cache()
interval_time =delay_time()
t = threading.Timer(interval_time, event_func)
t.setDaemon(True)
t.start()
# Take time
def delay_time():
# now time
now_time = datetime.datetime.now()
# tomorrow time
next_time = now_time + datetime.timedelta(days=+1)
next_year = next_time.date().year
next_month = next_time.date().month
next_day = next_time.date().day
# get tomorrow 00:00
next_time = datetime.datetime.strptime(str(next_year)+"-"+str(next_month)+"-"+str(next_day)+" 00:00:00","%Y-%m-%d %H:%M:%S")
# get secondes
delay_time =(next_time - now_time).total_seconds()return delay_time
def quit_sys(signum, frame):
sys.exit()
# Receive C
if __name__ =="__main__":try:
signal.signal(signal.SIGINT, quit_sys)
signal.signal(signal.SIGTERM, quit_sys)schedule_update()print("schedule_update server starting up...\nHit Ctrl-C to quit.\n")while1:
time.sleep(1)
except Exception as e:print(e)

So far this article on how Python implements the timer function is introduced. For more simple timer examples in Python, please search ZaLou.Cn

Recommended Posts

How Python implements the timer function
How Python implements the mail function
How Python implements FTP function
Python Print print timer function
How to use the round function in python
How to use the zip function in Python
How to use the format function in python
Python implements image stitching function
Python implements the brick-and-mortar game
How does python change the environment
Python simply implements the snake game
Python3 implements the singleton design pattern
How to save the python program
Python implements AI face change function
Python implements the steepest descent method
How to view the python module
Python implements the aircraft war project
How does python improve the calculation speed
Python implements the sum of fractional sequences
How to run id function in python
What is the id function of python
How does python call its own function
python implements the gradient method python the fastest descent method
How does Python handle the json module
How to learn the Python time module
What is the function body of python
python3 simply implements the combined design pattern
Python enumerate() function
Python function buffer
What is the function of adb in python
Python implements the shuffling of the cards in Doudizhu
Python implements the source code of the snake game
How to enter python through the command line
How about learning python at the age of 27?
How to install the downloaded module in python
Python how to move the code collectively right
Python implements Super Mario
Detailed explanation of the principle of Python function parameter classification
Python implements tic-tac-toe game
Detailed explanation of the principle of Python timer thread pool
Python custom function basics
How to practice after the python file is written
Join function in Python
Python built-in function -compile()
How does python call the key of a dictionary
Python implements man-machine gobang
How does python get input examples from the keyboard
Python function basic learning
Python data analysis-apply function
Python implements image stitching
How to understand the introduction of packages in Python
Python3 built-in function table.md
Python implements scanning tools
How python was invented
How does python handle the program cannot be opened
How does python judge the module installation is complete
Python implements threshold regression
Python implements minesweeper games
How Python parses XML
Python implements electronic dictionary
Python implements guessing game