Python 3.x 如何使用discord.py进行多处理?
我想做的一个例子是这样的Python 3.x 如何使用discord.py进行多处理?,python-3.x,discord.py,python-asyncio,Python 3.x,Discord.py,Python Asyncio,我想做的一个例子是这样的 i = 0 @bot.command() async def IncreaseI(ctx): global i while True: i += 1 sleep(5) @bot.command() async def PrintI(ctx): await ctx.send(f"I: {i}") 但是这不起作用。首先,您使用的是时间.sleep(),这会阻塞整个代码,请使用等待异步I
i = 0
@bot.command()
async def IncreaseI(ctx):
global i
while True:
i += 1
sleep(5)
@bot.command()
async def PrintI(ctx):
await ctx.send(f"I: {i}")
但是这不起作用。首先,您使用的是
时间.sleep()
,这会阻塞整个代码,请使用等待异步IO.sleep()
。其次,创建一个任务更好,管理起来更容易,下面介绍如何:
来自discord.ext导入任务
i=0
@tasks.loop(秒=5)
异步def increaseI():
全球i
i+=1
increaseI.start()#也可以在命令中启动
#你也可以
增加i.stop()
increaseI.cancel()
increaseI.restart()
#阅读文档了解更多方法