one , If you want to run a piece of code at a specified time , Refer to the following procedure .
import time from interval import Interval while True: # current time now_localtime =
time.strftime("%H:%M:%S", time.localtime()) # current time ( Expressed as a time interval ) now_time =
Interval(now_localtime, now_localtime) print(now_time) time_interval =
Interval("11:15:00", "15:50:00") print(time_interval) if now_time in
time_interval: print(" It's in this time interval ") print(" Part of code to execute ")

two , Another code executes code at regular intervals :
# introduce time package import time # Function definition def sleeptime(hour,min,sec): return hour*3600 +
min*60 + sec # Set automatic execution interval , I set it here 2s second = sleeptime(0,0,2) # Dead cycle while True:
# delayed time.sleep(second) time.sleep(second) time.sleep(second) # implement print ("do
action")

three , Every day 3 Point execution code :
''' Created on 2018-4-20 example : Every morning 3 Point execution func method ''' import datetime import
threading def func(): print("haha") # Loop call if needed , To add the following methods timer =
threading.Timer(86400, func) timer.start() # Get current time now_time =
datetime.datetime.now() # Get 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 3 Point time next_time =
datetime.datetime.strptime(str(next_year)+"-"+str(next_month)+"-"+str(next_day)+"
03:00:00", "%Y-%m-%d %H:%M:%S") # # Get yesterday's time # last_time = now_time +
datetime.timedelta(days=-1) # Get distance from tomorrow 3 Point time , In seconds timer_start_time = (next_time -
now_time).total_seconds() print(timer_start_time) # 54186.75975
# timer , Parameter is ( How long will it take to execute , In seconds , Method of execution ) timer = threading.Timer(timer_start_time, func)
timer.start()

four , Run at the specified time every day and stop at the specified time
from datetime import datetime, time import multiprocessing from time import
sleep # The program runs during the day 8:30 reach 15:30 night 20:30 reach before dawn 2:30 DAY_START = time(8, 30) DAY_END
= time(15, 30) NIGHT_START = time(20, 30) NIGHT_END = time(2, 30) def
run_child(): while 1: print(" Running child process ") def run_parent(): print(" Start parent process ")
child_process = None # Is there a child process while True: current_time = datetime.now().time()
running = False # Is the child process runnable if DAY_START <= current_time <= DAY_END or
(current_time >= NIGHT_START) or (current_time <= NIGHT_END): # The judgment time is within the runnable time
running = True # Within the time period, the child process is started if running and child_process is None:
print(" Start child process ") child_process = multiprocessing.Process(target=run_child)
child_process.start() print(" The child process started successfully ") # If the time is not recorded, the child process exits if not running and
child_process is not None: print(" Close child process ") child_process.terminate()
child_process.join() child_process = None print(" Subprocess closed successfully ") sleep(5) if __name__
== '__main__': run_parent()

Technology
©2019-2020 Toolsou All rights reserved,
Redis Underlying data structure A person who dissuades others from learning computer , Are not good people win10 System computer C Where's the disc ,c disc users where? (win10c Disk not found users) Freshman c Language student management system ( lower ) hospital WIFI Which family is strong ? utilize Python Script unlimited spoof girlfriend computer C language program design —— Student management system Byte runout - Test development practice - One side cool meridian python Run code at specified time 5 Best style Linux Server system recommendation Anti anxiety life after naked resignation , I believe everything will have the best arrangement