How can I run an async function using the schedule library?

后端 未结 5 937
广开言路
广开言路 2020-11-29 10:52

I\'m writing a discord bot using discord.py rewrite, and I want to run a function every day at a certain time. I\'m not experienced with async functions at all and I can\'t

5条回答
  •  离开以前
    2020-11-29 11:06

    Another option is to use apscheduler's AsyncIOScheduler, which works more naturally with async functions (such as send_channel). In your case, you can simply write something of the form:

    scheduler = AsyncIOScheduler()
    scheduler.add_job(send_channel, trigger=tr)
    scheduler.start()
    

    Where tr is a trigger object. You can use either IntervalTrigger with a 1-day interval and a start date at 21:57, or a CronTrigger.

    Note that at the end of your program it is recommended to call shutdown() on the scheduler object.

提交回复
热议问题