python - discord.py 异步功能阻塞
问题描述
我不是异步东西的专家,但我不明白这一点。我正在使用 TwitterAPI 为新帖子获取 Twitter API 流端点。我设置了频道,它应该会收听新帖子,而且效果很好。但是为什么我不能在 set_channel 函数运行时使用我的 foo 命令呢?我认为它是异步的......我可能理解完全错误的东西,无论如何。帮助将不胜感激。
@bot.command()
async def set_channel(ctx):
while True:
try:
iterator = api.request(
'statuses/filter', {'follow': user_ids}).get_iterator()
for item in iterator:
if 'text' in item:
await ctx.channel.send(item['text'])
elif 'disconnect' in item:
event = item['disconnect']
if event['code'] in [2, 5, 6, 7]:
raise Exception(event['reason'])
else:
break
except TwitterRequestError as e:
if e.status_code < 500:
raise
else:
pass
except TwitterConnectionError:
pass
@bot.command()
async def foo(ctx):
await ctx.channel.send("bar")
```
解决方案
推荐阅读
- php - 有没有办法在 PHP Twig 中比较字符串忽略大小写?
- python-3.x - 解析 HTML [ ] 输出
- php - 嘲弄“with”方法没有显示失败的原因
- python-3.x - 倾城报告未知未知 - NoN%
- python - 我如何使这个循环向后
- c# - 如何在 C# 中重新实现 PHP 7.3 数组排序方法?
- postgresql - 带有纯 SQL 的 JOOQ EXISTS 子句
- c# - 获取“使用未分配的局部变量”C#错误
- reactjs - ./node_modules/mapbox-gl/dist/mapbox-gl.css 模块构建失败:TypeError:无法读取未定义的属性“toFixed”
- javascript - iframe 问题:拒绝在 iframe 中显示问题