Answer the question
In order to leave comments, you need to log in
How to run a method every minute?
Good day. Tell me, how can I run a method (function) in my program every minute?
Answer the question
In order to leave comments, you need to log in
The answer depends on a number of factors.
1. Should the program do something else? If yes, what exactly?
2. How many such periodic methods will exist? Strictly one, or maybe more? Should they have different periods?
If the program is synchronous, and there is only one method, then a simple trick can be used:
import time
stop = False
def proc_to_call():
global stop
#Это твой периодический вызов
#когда нужен останов, делаешь stop = True
pass
while not stop:
time.sleep(60)
proc_to_call()
#код после цикла выполнится только после прерывания цикла.
while True:
schedule.run_pending()
time.sleep(1)
import threading
class MyJob(threading.Thread):
def __init__(self):
super().__init__(self)
self.stop = threading.Event()
def proc_to_call(self):
#Это твой периодический вызов
pass
def run(self):
while not self.stop.wait(60):
self.proc_to_call()
job = MyJob()
job.start()
#код основного потока продолжает выполняться после этого
#когда нужен останов, делаешь
job.stop.set() #сигналим об останове
job.join() #ждём завершения потока
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question