Python erstellen named Timer
try:
from threading import Timer
except ImportError:
from threading import _Timer as Timer # Python <3.3
def named_timer(name, interval, function, *args, **kwargs):
"""Factory function to create named Timer objects.
Named timers call a function after a specified number of seconds:
t = named_timer('Name', 30.0, function)
t.start()
t.cancel() # stop the timer's action if it's still waiting
"""
timer = Timer(interval, function, *args, **kwargs)
timer.name = name
return timer
if __name__ == '__main__':
def func():
print('func() called')
timer = named_timer('Fidgit', 3, func)
print('timer.name: {!r}'.format(timer.name)) # -> timer.name: 'Fidgit'
timer.run() # Causes "func() called" to be printed after a few seconds.
Scarlet Macaw