Reputation: 123
Is it possible to schedule a function to execute at every xx millisecs in python,without blocking other events/without using delays/without using sleep ?
What is the best way to repeatedly execute a function every x seconds in Python? explains how to do it with sched module, but the solution will block the entire code execution for the wait time(like sleep).
The simple scheduling like the one given below is non blocking, but the scheduling works only once- rescheduling is not possible.
from threading import Timer
def hello():
print "hello, world"
t = threading.Timer(10.0, hello)
t.start()
I am running the python code in Raspberry pi installed with Raspbian.Is there any way to either schedule the function in non blocking way or trigger it using 'some features' of the os?
Upvotes: 6
Views: 8930
Reputation: 1816
As an alternative where you cannot use threading
, like in some code generators where you have little freedom, I have used the elapsed
function defined below and tested with three non blocking tasks, one and two in parallel, then three:
#!/usr/bin/env python3
# timer.py
from time import time #, sleep
from pprint import pprint
t = dict()
def elapsed(name: str, duration: float):
if name in t:
return (time() >= t[name])
else:
t[name] = time() + duration
return False
if __name__ == "__main__":
finished = False
fone = ftwo = fthree = True
start = time()
qt = 0
print(f"Start at {start:0.2f} : one for 1s and two for 2s together, then three for 3s")
while not finished:
if len(t) > qt: #int(time() * 10) % 10 == 0:
pprint(t)
qt = len(t) #sleep(0.01)
one = elapsed("one", 1)
two = elapsed("two", 2)
three = False
if one and fone:
print(f"one finished in {time() - start:0.2f} seconds")
fone = False
if two and ftwo:
print(f"two finished in {time() - start:0.2f} seconds")
ftwo = False
if one and two:
three = elapsed("three", 3)
if three and fthree:
print(f"three finished in {time() - start:0.2f} seconds")
fthree = False
if three:
finished = True
Output:
./timer.py
Start at 1656828388.84 : one for 1s and two for 2s together, then three for 3s
{'one': 1656828389.8441286, 'two': 1656828390.84413}
one finished in 1.00 seconds
two finished in 2.00 seconds
{'one': 1656828389.8441286, 'three': 1656828393.844173, 'two': 1656828390.84413}
three finished in 5.00 seconds
Upvotes: 0
Reputation: 879691
You can "reschedule" the event by starting another Timer
inside the callback function:
import threading
def hello():
t = threading.Timer(10.0, hello)
t.start()
print "hello, world"
t = threading.Timer(10.0, hello)
t.start()
Upvotes: 11