如何解决如何在 discord.py 中创建多个循环后台任务不重写
我一直在尝试为我的 discord.py 机器人添加一个循环后台任务系统,但是一旦一个任务开始运行,它就会阻止其他任务。我曾尝试到处寻找答案,但要么他们使用的是 discord.py 的重写版本,要么他们只有一项任务。
我试图让任务并行运行,以便所有任务可以同时运行。
这是我的代码:
# hiding imports
class BackgroundTasksCollection:
def __init__(self,client: discord.Client):
self.client = client
async def start_tasks(self):
tasks = []
for name in dir(self):
if name.startswith("task_"):
print(name)
tasks.append(getattr(self,name))
[await self.client.loop.create_task(task()) for task in tasks]
# tasks
async def task_1(self): # only this will run because the tasks are started in alphabetical order
await self.client.wait_until_ready()
while not self.client.is_closed():
await asyncio.sleep(60 * 60)
# ...
async def task_2(self):
await self.client.wait_until_ready()
while not self.client.is_closed():
await asyncio.sleep(1)
# ...
async def task_3(self):
await self.client.wait_until_ready()
while not self.client.is_closed():
await asyncio.sleep(30)
# ...
class MyClient(discord.Client):
def __init__(self,**options):
super().__init__(**options)
self.tasks = BackgroundTasksCollection(self)
async def on_ready(self):
# ...
await self.tasks.start_tasks()
# handle message,errors,etc.
解决方法
您可能应该查看 documentation
中的任务的任务这是来自上面链接的文档:
from discord.ext import tasks,commands
class MyCog(commands.Cog):
def __init__(self,bot):
self.index = 0
self.bot = bot
self.printer.start()
def cog_unload(self):
self.printer.cancel()
@tasks.loop(seconds=5.0)
async def printer(self):
print(self.index)
self.index += 1
@printer.before_loop
async def before_printer(self):
print('waiting...')
await self.bot.wait_until_ready()
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。