python - python discord.py 后台任务停止主函数
问题描述
使用:discord.py 我试图运行后台任务。但是在它启动后,像 on_message 这样的主函数停止工作。
代码
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)
解决方案
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')
推荐阅读
- svg - 如何制作我的 svg 图像圆,该圆目前是正方形,半径为无聊
- filter - 在 weka 中为由数字和字母组成的 id 选择什么过滤器
- sql-server - MS SQL Server - 复制数据库的初始数据匿名化
- spring-boot - Kafka 消费者 LeaveGroup 请求协调器
- javascript - 谷歌开发人员的 Puppeteer 代码是否有问题:custom-event.js?
- azure - 蔚蓝阿皮姆。根据 openapi 定义在模式中创建操作
- vue.js - Vue无法渲染大括号+值
- ios - Swift 中静态变量的生命周期是多久?
- android - 导致 Appium 无法正确识别元素的可能因素有哪些?
- flutter - 无法使用颤振包更好的播放器播放 .m3u8 文件