Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 如何使用discord.py进行多处理?_Python 3.x_Discord.py_Python Asyncio - Fatal编程技术网

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()
#阅读文档了解更多方法