注册定时任务

# 简单说明

在 AmiyaBot 运行期间,定时执行的循环任务。

# 注册任务

示例,注册一个每 10 秒执行一次的任务

from core import bot

@bot.timed_task(each=10)
async def _():
    print('过了 10 秒')
1
2
3
4
5

# bot.timed_task 注册函数

timed_task 函数是 bot 模块内的函数装饰器,接受 eachcustom 两个参数。

参数名 类型 释义 默认值
each Int 循环执行间隔时间,单位(秒)
custom Callable 自定义循环规则

注意

此定时事件为非严格定时,因为执行协程会产生切换的耗时。所以此注册器定义的循环时间为"约等于"。

custom 自定义循环规则

custom 函数是一个协程,每秒会被调用一次,接受一个 int 参数,参数为 AmiyaBot 的运行时间总秒数。
如果传入了自定义循环规则,each 将会失效。

from core import bot


async def my_check(t: int):
    ...
    return True


@bot.timed_task(custom=my_check)
async def _():
    ...
1
2
3
4
5
6
7
8
9
10
11
上次更新: 12/14/2022, 12:49:25 PM