python - Asyncio/Discord.py:使用 loop.create_task() 发出任务,其优先级/速度与 await 相同
问题描述
loop.create_task()
使用或发出任务时似乎存在很大的时间差异await
。在下面的示例 discord bot 中,输入"!q"
任何文本通道几乎都会立即停止 bot,"q"
在 python 控制台中输入最多需要 2 分钟。我希望发出的任务以相同的速度执行,就像用 await 发出的一样。我想知道,为什么会有这么大的时差。我是否缺少优先级系统,或者是否有其他方法可以从线程发出异步任务?
示例代码:
import discord, threading
class BotClient(discord.Client):
async def halt(self):
await self.close()
print("[Discord Client stopped]")
async def on_message(self, message):
if message.content == "!q":
await self.halt()
class InteractiveThread(threading.Thread):
def __init__(self, client):
super().__init__()
self.client = client
def run(self):
cmd = input()
if(cmd=="q"):
self.client.loop.create_task(self.client.halt())
client = BotClient()
IAT = InteractiveThread(client)
IAT.start()
client.run(SECRET_DISCORD_TOKEN)
IAT.join()
(SECRET_DISCORD_TOKEN 必须替换为您的令牌)
解决方案
推荐阅读
- selenium - 如何修复硒中的“部分链接类名称查找”
- .net - Azure Webjobs 3.0 使用手动触发器从 blob 读取
- laravel - 存储文件路径问题它在服务器端重复
- php - PHP 检查变量是真还是假
- python - 在 Pandas 系列上使用 pytz 转换时区
- r - 如果数据框没有列名,如何使用收集函数选择 0 列
- node.js - 如何使用 downloadhelper 插件保护 s3 文件不被下载
- android - 限制在 CustomView 中使用 WRAP_CONTENT 和 MATCH_PARENT
- docker - 如何删除
构建成功后的图片 - angular - 为什么我的代码中没有订阅我的 Observables?