Working hours and start

The clock works constantly and starts another function every 5 seconds.

Please let me know how to do this.

Thanks a bunch

+3
source share
3 answers
>>> import sched, time
>>> s = sched.scheduler(time.time, time.sleep)
>>> def print_time():
...     s.enter(5, 1, print_time, ())
...     print "From print_time", time.time()
... 
>>> s.enter(0, 1, print_time, ())
Event(time=1265846894.4069381, priority=1, action=<function print_time at 0xb7d1ab1c>, argument=())
>>> s.run()
From print_time 1265846894.41
From print_time 1265846899.41
From print_time 1265846904.42
From print_time 1265846909.42
+4
source
import time

while True:
    time.sleep(5)
    someFunction()

As the gnibbler says, the interval will actually be (5 seconds + the time it takes to start someFunction). If you need exactly 5 seconds:

targetTime = time.time()
while True:
    someFunction()
    targetTime += 5
    sleepTime = targetTime - time.time()
    if sleepTime>0:
        time.sleep(sleepTime)
+1
source

You can use the scheduler from the sched module .

Just execute the scheduled function every time it completes.

+1
source

Source: https://habr.com/ru/post/1732272/


All Articles