Background Tasks
Intro#
Panther is going to run the background tasks
as a thread in the background on startup if you set the BACKGROUND_TASKS
to True
Usage#
-
Add the
BACKGROUND_TASKS = True
in theconfigs
-
Import the
background_tasks
frompanther.background_tasks
:from panther.background_tasks import background_tasks
-
Create a
task
from panther.background_tasks import background_tasks, BackgroundTask def do_something(name: str, age: int): pass task = BackgroundTask(do_something, name='Ali', age=26)
-
Now you can add your task to the
background_tasks
from panther.background_tasks import background_tasks, BackgroundTask def do_something(name: str, age: int): pass task = BackgroundTask(do_something, name='Ali', age=26) background_tasks.add_task(task)
Options#
-
Interval#
You can set custom
interval
for thetasks
, let's say we want to run thetask
below for3 times
.from panther.background_tasks import BackgroundTask, background_tasks def do_something(name: str, age: int): pass task = BackgroundTask(do_something, name='Ali', age=26).interval(3) background_tasks.add_task(task)
-
Schedule#
BackgroundTask
has some methods toschedule
the run time, (Default value of them is1
) every_seconds()
every_minutes()
every_hours()
every_days()
every_weeks()
at()
# Set Custom Time-
on()
# Set Custom Day Of WeekYou can pass your custom value to them too,
Example: Run Every 4 days:
every_days(4)
. -
Time Specification#
You can set a custom
time
to tasks too
let's say we want to run the task
below every day on 8:00
o'clock.
from datetime import time
from panther.background_tasks import BackgroundTask, background_tasks
def do_something(name: str, age: int):
pass
task = BackgroundTask(do_something, name='Ali', age=26).every_days().at(time(hour=8))
background_tasks.add_task(task)
-
Day Of Week Specification#
Now we want to run thetask
below every 2 week onmonday
, on8:00
o'clock.
from datetime import time
from panther.background_tasks import BackgroundTask, background_tasks
def do_something(name: str, age: int):
pass
task = BackgroundTask(do_something, name='Ali', age=26).every_weeks(2).on('monday').at(time(hour=8))
background_tasks.add_task(task)
Notice#
-
The
task
function can besync
orasync
-
You can pass the arguments to the task as
args
andkwargs
def do_something(name: str, age: int): pass task = BackgroundTask(do_something, name='Ali', age=26) or task = BackgroundTask(do_something, 'Ali', age=26) or task = BackgroundTask(do_something, 'Ali', 26)
-
Default of interval() is 1.
-
The -1 interval means infinite,
-
The
.at()
only useful when you are using.every_days()
or.every_weeks()
-
The
.on()
only useful when you are using.every_weeks()