【问题标题】:python discord.py background task stop main funcpython discord.py 后台任务停止主函数
【发布时间】:2020-09-15 14:36:19
【问题描述】:
使用:discord.py
我试图运行后台任务。但是在它启动后,像 on_message 这样的 main func 就停止工作了。
代码
async def on_ready():
print('x')
x = "true"
client.loop.create_task(my_background_task(x))
async def my_background_task(x):
while True:
#do stuff
await asyncio.sleep(3)
【问题讨论】:
标签:
python
discord.py
python-asyncio
【解决方案1】:
import discord
client = discord.Client()
@client.event
async def on_ready():
#your on_ready code here
@client.event
async def on_message(message):
#your on_message code here
async def my_background_task(x):
await client.wait_until_ready()
while not client.is_closed():
#do stuff
await asyncio.sleep(3)
client.loop.create_task(my_background_task(True))
client.run('token')