Non-blocking event scheduling in python

Is it possible to schedule a function to execute at every xx milliseconds in python without blocking other events / without using delays / without using sleep?

What is the best way to execute a function multiple times every x seconds in Python? explains how to do this with the scheduling module, but the solution blocks all code execution for the timeout (like sleep).

Simple scheduling like the one below does not block, but scheduling only works once - no reconfiguration is possible.

from threading import Timer
def hello():
    print "hello, world" 

t = threading.Timer(10.0, hello)
t.start() 

      

I am running python code in Raspberry pi installed with Raspbian. Is there a way to schedule the function in a non-blocking way, or call it with "some functions" os?

+3


source to share


1 answer


You can "wrap" the event by triggering another one 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() 

      

+8


source







All Articles