How to schedule recurring tasks¶
Recurring tasks can be scheduled in two ways:
@bus.client.every()decorator – Will execute a function or coroutine at a given interval
@bus.client.schedule()decorator – Similar to
every(), but takes complex schedules as provided by the schedule library.
Simple recurring tasks using
Lightbus natively supports simple recurring tasks using the
# bus.py import lightbus bus = lightbus.create() @bus.client.every(seconds=1) def do_it(): print("Hello!")
The interval can be specified using the
also_run_immediately=True to execute the function/coroutine immediately, as well as
at the given interval.
Complex schedules using
Lightbus also supports using schedules specified using the schedule library. This allows for schedules such as 'every Monday at 1am', rather than simple intervals. For example:
import lightbus import schedule bus = lightbus.create() # Run the task every 1-3 seconds, varying randomly @bus.client.schedule(schedule.every(1).to(3).seconds) def do_it(): print("Hello using schedule library")