TimerThread is
- A lightweight task scheduling timer
- written in Python (3.7+) Standard Library
TimerThread supports to
- schedule task execution after a given delay
- schedule recurring task execution
- run in the background
- use
@taskdecorator to define task
Define your function, now(cost) as an example:
import time
def now(cost=1):
time.sleep(cost)
print( time.strftime('%Y-%m-%d %H:%M:%S %Z', time.localtime()) )Create a TimerThread scheduler and start it:
import timerthread
timer = timerthread.Scheduler('recur', 3, now, args=(1,))
timer.start()Shutdown the scheduler:
timer.cancel()Use @task decorator to define your function, then schedule it and start the scheduler, now(cost) as an example:
import time
import timerthread
@timerthread.task('recur', 3)
def now(cost=1):
time.sleep(cost)
print( time.strftime('%Y-%m-%d %H:%M:%S %Z', time.localtime()) )
timer = now.sched(cost=1)
timer.start()When you'd like to cancel the recurring execution, shutdown the scheduler as usual:
timer.cancel()$ pip install timerthreadclass timerthread.Scheduler(trigger, interval, fn, args=(), kwargs={})trigger must be 'delay' or 'recur'.
-
stoppedThe scheduler is stopped or not,
True(default) orFalse. -
resultThe execution result,
{}as default. -
start()Let scheduler start executing your function as scheduled in the background.
-
cancel()Shutdown the scheduler.
class timerthread.task(trigger, interval)trigger must be 'delay' or 'recur'.
-
Use
@taskdecorator to define your function, then schedule it and start the scheduler:@timerthread.task(trigger, interval) def fn(args, kwargs): pass timer = fn.sched(*args, **kwargs)
fn.sched(*args, **kwargs)returnstimerthread.Schedulerinstance.