小编Mic*_*ard的帖子

如何使用调度库运行异步函数?

我正在使用 discord.py rewrite 编写一个不和谐的机器人,我想每天在某个时间运行一个函数。我完全没有使用异步函数的经验,我不知道如何在不使用“await”的情况下运行一个。这只是我的一段代码,这就是为什么有些东西可能没有定义。

async def send_channel():
    try:
        await active_channel.send('daily text here')
    except Exception:
        active_channel_id = None
        active_channel = None

async def timer():
    while True:
        schedule.run_pending()
        await asyncio.sleep(3)
        schedule.every().day.at("21:57").do(await send_channel())

@bot.event
async def on_ready():
    print("Logged in as")
    print(bot.user.name)
    print(bot.user.id)
    print("------")

    bot.loop.create_task(timer())
Run Code Online (Sandbox Code Playgroud)

使用该schedule.every().day.at("00:00").do()函数,当我await send_channel()输入以下参数时出现此错误.do()

self.job_func = functools.partial(job_func, *args, **kwargs) TypeError: 第一个参数必须是可调用的

但是当我不使用 await 并且我只有send_channel()参数时,我会收到此错误:

运行时警告:从未等待过协程“send_channel”

我不太擅长编程,所以如果有人可以尝试为我简化它,那就太棒了。

谢谢

python schedule python-3.x async-await discord.py-rewrite

9
推荐指数
3
解决办法
1万
查看次数