Tasks¶
Tasks are background processes that can be used to asynchronously run code with a specified trigger.
How They Work¶
Tasks work by creating an asyncio.Task
to run a loop to check if the task is ready to be run based on the provided trigger. Using them is fairly easy, and the easiest way is via Decorators.
Decorators are by far the easier way to run tasks, with very simple syntax to get started.
1 2 3 4 5 |
|
- This will create a task that runs every 10 minutes
You can also manually register tasks
1 2 3 4 5 6 |
|
By default, there are a few triggers available to the user.
These triggers run every set interval.
1 2 3 4 5 |
|
These triggers are similar to IntervalTriggers, but instead run when a specified datetime is reached.
1 2 3 4 5 6 7 8 |
|
- This create a
datetime
object for January 1, 2100 - This uses the
future
object to create aTask
scheduled for January 1, 2100
These triggers are similar to DateTriggers, but trigger daily at the specified hour, minute, and second.
1 2 3 4 5 |
|
- This creates a task to run at midnight every day
These triggers are special, in that you can pass in a list of different triggers, and if any of them are triggered, it runs the function.
1 2 3 4 5 |
|
- This creates a task that triggers at either 5 AM local time or 5 PM local time
Starting a task¶
To start a task that has been created, you need to run the Task.start()
method from an async
function. A good place to do this is during on_startup
:
1 2 3 4 5 6 7 8 9 10 11 |
|
- See Events for more information
1 2 3 4 5 6 7 8 9 10 11 |
|