Package: timesched
TIMESCHEDThetimeschedPython module provides a simple time event scheduler. It
is implemented upon the standard Pythonschedmodule and is
used in a similar way, but provides a nicer and more modern and
convenient programming interface. Apart from normal oneshot and repeat
timers, it can run timers at a given time of day, and for the given days
of week, providing simple cron-like functionality. It requires only
Python 3.6 or later, and the standard library. The latest version of
this document and code is available athttps://github.com/bulletmark/timesched.classtimesched.Scheduler(timefunc=time.time,delayfunc=time.sleep)Refer to the class description for Pythonsched.schedulerwhich is used in theinternal
implementationof this class.Schedulerinstances have the following methods.Create one-shot timeroneshot(time,priority,func,*args,**kwargs)time: expiry time relative to now, or absolute time. Type can be one of:intat given relative seconds after now,datetime.timedelta()at given relativetimedelta()after now,datetime.time()at giventime()after now,datetime.datetime()at given absolutedatetime(),datetime.date()at given absolutedate(),priority: int value, lower value is higher priority. Referdescription.funcandargsandkwargs: user function and arguments to call
when timer is invoked.Returns atimerID which can be used to cancel the timer usingcancel(timer).Create repeating timerrepeat(period,priority,func,*args,**kwargs)period: period for timer. Type can be one of:intat each given seconds after now,datetime.timedelta()at each giventimedelta()after now,datetime.time()at giventime()each day after now. E.g. at
10:30 every day for simple daily "cron-like" functionality.priority: int value, lower value is higher priority. Referdescription.funcandargsandkwargs: user function and arguments to call
when timer is invoked.Returns atimerID which can be used to cancel the timer usingcancel(timer).Note that forintandtimedelta()periods, the specified period is
the delay between the end of the callback function and when it is called
again so the actual period will slowly "creep" by the run time of the
callback. Many applications are not concerned about this distinction but if
necessary you can instead invoke aoneshot()absolute timer between
each call.Create one-shot timer on next given dayCall this withtime=datetime.time()to invoke aoneshot()at the
giventimeon the next day from the set of givendaysof the week.oneshot_on_days(days,time,priority,func,*args,**kwargs)days: A list/set/sequence/range of integers 0-6 where 0 = Monday to
6 = Sunday. e.g. [0] means only invoke timer on a Monday, [0,6] = only
invoke on Monday and Sunday, etc. Usingdays=range(7)is same as
calling ordinaryoneshot().Alternately, you can specifydaysas a string "MTWTFSS" where each
char is upper case if the day is to be set, and lower case if not.
E.g. "MTWTFss" is the working week Mon-Fri, "mtwTfss" is Thu only,
etc. A utility function to explicitly parse this string into a set of
integers is available astimesched.parse_days(string_arg)if you
need.Remaining parameters and return type are same asoneshot().Create repeating timer on given daysCall this withtime=datetime.time()to invoke arepeat()at the
givenperiodon each of the givendaysof the week.repeat_on_days(days,period,priority,func,*args,**kwargs)days: parameter same asoneshot_on_days().Remaining parameters and return type are same asrepeat().Return count of active timerscount()Returns the count of timers currently active. A timer is considered
active while it is pending and until it's callback function has
completed, unless it is explicitly cancelled.Cancel timercancel(timer)Remove the timer withtimerID. If the timer is not currently active,
this method will raise a ValueError.Run schedulerrun(blocking=True)Invokes the basescheduler.run(). Refer full
description atPython sched
module.EXAMPLES#!/usr/bin/python3'Very simple examples'importtimeschedfromdatetimeimportdatetime,time# Job function to be called for each timerdefjob(jobid):print(f'Job{jobid}called at{datetime.now()}')# Create a schedulers=timesched.Scheduler()# Execute job() once in 5 secs times.oneshot(5,0,job,1)# Execute job() every 2 secss.repeat(2,0,job,2)# Execute job() at 10:30am every work day (not weekends)s.repeat_on_days('MTWTFss',time(10,30),0,job,3)# Run scheduler, will block until no timers left runnings.run()DIFFERENCES TO STANDARD SCHED MODULEThetimeschedmodule is internally implemented using the standard
Pythonschedmodule
but differs in the following ways. Note that theschedimplementation,
methods, and attributes are not directly exposed in the public interface.Providesoneshot()andrepeat()methods to conveniently accept
standarddatetime.datetime(),datetime.date(),datetime.time(),datetime.timedelta(),
and also integer time arguments, based automatically on the type of the
passed time argument.Therepeat()method sets itself up to be automatically invoked again
at the next repeat interval, unlikeschedwhich only provides aoneshot()equivalent method [i.e.enter()orenterabs()] so the user
does not need to explicitly set up the next timer.Provides a convenient way to schedule a timer at a given time each
day to give simple daily "cron-like" functionality, e.g.s.repeat(datetime.time(hour=10, minute=30), f)to periodically
activate a timer at 10:30 every day.Further to the aboverepeat()which can run at a given time every
day, you can userepeat_on_days()to specify a given time on a set
of given days, e.g.s.repeat_on_days('MTWTFss', datetime.time(hour=10, minute=30), f)to run the timer at 10:30 each
workday only (Mon to Fri). Alternatelys.repeat_on_days(range(5), datetime.time(hour=10, minute=30), f)gives the same result.Consistent with modern Python, allows user to plainly specify*argsand**kwargsdirectly in timer setup call rather than in a tuple as
legacyschedrequires.Does not provide theenter()orenterabs()methods. Use the superioroneshot(),repeat(),oneshot_on_days(), orrepeat_on_days()methods instead.Provides a more specificcount()method instead ofempty().Does not provide thequeueattribute.Usestime.timeinstead oftime.monotonicas the defaulttimefuncfor the internalscheduler.
This is to be compatible withdatetime.datetime.timestamp()which is used internally.INSTALLATIONArch Linux users can installtimesched from the
AUR.timeschedisavailable on PyPIso install the usual way, e.g:pip3installtimeschedOr explicitly fromgithub:gitclonehttps://github.com/bulletmark/timesched.gitcdtimesched
sudopip3install.