This is another article of csdn: python timer, 3:00 a.m. every day Method update
python implementation of timing automatic start code thread method (daily update, crawler, etc.)
This paper uses the method of threading.Timer(seconds, fun) to realize cycle start
Obtain the current time through methods such as datetime.datetime.now() and make correction judgment at the same time
The effect of timing self starting function
If you want instant food, modify marktime directly
Then add your code to func or call your function
Annotated edition
import datetime import threading marktime=" 18:02:20" # Modify: change the above marktime to the self starting time you need # (don't delete the space) please modify the time according to the above format. Random modification will result in an error # Operation function def func(): # Add your function here print("haha") #If you need a circular call, add the following methods timer = threading.Timer(86400, func) # 86400 is 3600 * 24, you know what I mean timer.start() # preFun preprocessing function judges the time of the first startup today or the next day, and then it is convenient for correct self startup def preFun(): # Get the current time now_time = datetime.datetime.now() marktimes = datetime.datetime.strptime(str(now_time.date()) + marktime, "%Y-%m-%d %H:%M:%S") # marktimes is a datetime ized time data type # 2020-03-13 17:35:26.772379 marktimes is similar to the structure on the left # Is today's time expected if (now_time <= marktimes): next_time = marktimes print("today" + marktime + 'Execution code') else: # Start tomorrow next_time = now_time + datetime.timedelta(days=+1) print("tomorrow" + marktime + 'Execution code') # This can be optimized below. I'm lazy Ha ha ha ha next_year = next_time.date().year next_month = next_time.date().month next_day = next_time.date().day next_time = datetime.datetime.strptime(str(next_year) + "-" + str(next_month) + "-" + str(next_day) + marktime, "%Y-%m-%d %H:%M:%S") # Next time updates the next time obtained to the time in seconds # Popular science is that the second obtained by the time.time() function is convenient to calculate, otherwise, it needs crazy base conversion # Returns the timestamp of the current time (floating-point seconds since the 1970s). # Gets the time from the next marktime, in seconds timer_start_time = (next_time - now_time).total_seconds() return timer_start_time def main(): timer_start_time=preFun() # Put in the processing time of and and then the thread will be after start() # Will start the code in your func after the specified number of seconds timer = threading.Timer(timer_start_time, func) # Thread of operation timer.start() print('Start after cold start func Time',timer_start_time) pass if __name__ == '__main__': main()
Concise Edition
import datetime import threading marktime=" 18:02:20" # Operation function def func(): # Add your function here print("haha") timer = threading.Timer(86400, func) timer.start() # preFun preprocessing function def preFun(): now_time = datetime.datetime.now() marktimes = datetime.datetime.strptime(str(now_time.date()) + marktime, "%Y-%m-%d %H:%M:%S") if (now_time <= marktimes): next_time = marktimes print("today" + marktime + 'Execution code') else: # Start tomorrow next_time = now_time + datetime.timedelta(days=+1) print("tomorrow" + marktime + 'Execution code') next_year = next_time.date().year next_month = next_time.date().month next_day = next_time.date().day next_time = datetime.datetime.strptime(str(next_year) + "-" + str(next_month) + "-" + str(next_day) + marktime, "%Y-%m-%d %H:%M:%S") timer_start_time = (next_time - now_time).total_seconds() return timer_start_time def main(): timer_start_time=preFun() timer = threading.Timer(timer_start_time, func) timer.start() print('Start after cold start func Time',timer_start_time) pass if __name__ == '__main__': main()
summary
The code can be used but the performance is not tested There is also a thread interrupt is to close the process, too primitive method seems very inelegant. If you are interested, you can modify it yourself.