Meinen Sie etwas wie pygame.time.Clock()
?
Ausführen function1()
alle 10 Sekunden, bis 100 Sekunden vergangen sind:
import logging
import pygame
logging.basicConfig(format='%(asctime)s %(message)s', datefmt='%H:%M:%S')
def function1():
logging.warning("write logs")
fps = .1 # call function1() every 10 seconds
maxtime = 100 # finish in 100 seconds
clock = pygame.time.Clock()
for _ in range(int(maxtime*fps + .5)):
function1()
clock.tick(fps) # at most `fps` function1() calls per second
Ausgabe
11:44:05 write logs
11:44:15 write logs
11:44:25 write logs
11:44:35 write logs
11:44:45 write logs
11:44:55 write logs
11:45:05 write logs
11:45:15 write logs
11:45:25 write logs
11:45:35 write logs
Ähnliches wurde mit threading.Timer()
:
import logging
from time import time as timer
from threading import Timer
logging.basicConfig(format='%(asctime)s %(message)s', datefmt='%H:%M:%S')
def function1(interval, maxtime):
logging.warning('write logs')
if timer() < maxtime:
Timer(interval, function1, (interval, maxtime)).start()
function1(10, timer()+100)
Anmerkung: jeder Timer()
erzeugt einen neuen Thread.
09:06:31 write logs
09:06:41 write logs
09:06:51 write logs
09:07:01 write logs
09:07:11 write logs
09:07:21 write logs
09:07:31 write logs
09:07:41 write logs
09:07:51 write logs
09:08:01 write logs
09:08:11 write logs