Home > other >  According to the computer clock, in five minutes each hour. 10 minutes. 15 minutes,... 45 minutes an
According to the computer clock, in five minutes each hour. 10 minutes. 15 minutes,... 45 minutes an

Time:11-30

Based on computer clock, at each hour of 5 minutes, 10 minutes, 15 minutes,,,,,,, 45 minutes and 50 minutes, 55, minutes, 00 minute began to run the program,

For example:
# 00:00
Print (" hello ")
# 00:05
Print (" hello ")
# 00:10
Print (" hello ")
# 00:15
Print (" hello ")
# 00:20
Print (" hello ")
*
*
*
*
# 00:50
Print (" hello ")
# 00:55
Print (" hello ")
# 01:00
Print (" hello ")



Permanent cycle

CodePudding user response:

With a datetime

 import datetime 
Instant=1 # this variable is used to ensure that print only once in a interval
While True:
Now=datetime. Datetime. Now ()
If now. The 5 minute %==0 and now the minute!=instant:
Instant=now. Minute
Print (" Hello, world ")

CodePudding user response:

CPU usage is too high

CodePudding user response:

The from threading import Timer

A timer, a minute time

Perfect, and then open two timers, the first is to calculate the next time (0,5,10) time difference, to start a second timer, the second is the work, set to 5 minutes (300 seconds),

CodePudding user response:

refer to the second floor lwq2677 response:
CPU use too much

That is:
1. Add a time. Sleep (), to test, but the cycle time very naive
 import datetime, time 
Instant=1
Now=datetime. Datetime. Now ()
Time. Sleep (300 - (now. Minute % 5) * 60 - now in the second - 1)
While True:
Now=datetime. Datetime. Now ()
If now. The 5 minute %==0 and now the minute!=instant:
Instant=now. Minute
Print (now the hour, ":", now the minute, ":", now the second)
Print (" Hello, world ")
Time. Sleep (299)

2. Such as the upstairs said, opening a thread, timer timing, no cycle
 import threading, datetime 
Def doItRepeatly () :
DoItRepeatly, threading. The Timer (300) start ()
Print (" Hello world ")

Now=datetime. Datetime. Now ()
First=300 - (now. Minute % 5) * 60 - now the second # for the first point
DoItRepeatly, threading. The Timer (first) start ()

This you need to monitor: 1. The number of threads, GC should automatically recycling abandoned old threads,
2. The heap usage, their test