Quart Web 应用程序上的 asyncio Discord bot 会话超时
asyncio Discord bot session timeout on Quart web app
我正在尝试在 Quart 上实现与 Web 应用 运行 的不和谐机器人连接。
我尝试使用 发布的其中一种解决方案,它可以工作一段时间,但大约 24 小时后应用程序中断返回 asyncio.exceptions.TimeoutError
data = await state.http.send_message(channel.id, content, tts=tts, embed=embed,
File "/path/to/my/venv/lib/python3.9/site-packages/discord/http.py", line 185, in request
async with self.__session.request(method, url, **kwargs) as r:
File "/path/to/my/venv/lib/python3.9/site-packages/aiohttp/client.py", line 1117, in __aenter__
self._resp = await self._coro
File "/path/to/my/venv/lib/python3.9/site-packages/aiohttp/client.py", line 619, in _request
break
File "/path/to/my/venv/lib/python3.9/site-packages/aiohttp/helpers.py", line 656, in __exit__
raise asyncio.TimeoutError from None
asyncio.exceptions.TimeoutError
相关代码:
import discord
import asyncio
from quart import Quart, request, Response
app = Quart(__name__)
client = discord.Client()
@app.before_serving
async def before_serving():
loop = asyncio.get_event_loop()
await client.login(os.getenv('TOKEN'))
loop.create_task(client.connect())
@app.route('/webhook', methods=['POST'])
async def webhook():
...
await channel.send(content, embed=embed)
...
如何让客户端循环保持一天以上?
有没有办法防止客户端会话断开连接,或者我应该定期重新连接它?
我咳嗽 asyncio.TimeoutError 并设法重新启动会话:
async def discord_post(channel, message, embed):
try:
await channel.send(message, embed=embed)
except asyncio.TimeoutError:
await connect_client()
await channel.send(message, embed=embed)
@app.before_serving
async def connect_client():
loop = asyncio.get_event_loop()
await client.login(os.getenv('DISCORD_TOKEN'))
loop.create_task(client.connect())
@app.route('/webhook', methods=['POST'])
async def webhook():
...
await discord_post(channel, text, embed)
...
但是处理重启需要将近 300 秒,之后所有请求都会被立即处理并且 nginx 一直返回 499,即使代码现在正确执行,但我想这完全是另一个问题
我正在尝试在 Quart 上实现与 Web 应用 运行 的不和谐机器人连接。
我尝试使用
data = await state.http.send_message(channel.id, content, tts=tts, embed=embed,
File "/path/to/my/venv/lib/python3.9/site-packages/discord/http.py", line 185, in request
async with self.__session.request(method, url, **kwargs) as r:
File "/path/to/my/venv/lib/python3.9/site-packages/aiohttp/client.py", line 1117, in __aenter__
self._resp = await self._coro
File "/path/to/my/venv/lib/python3.9/site-packages/aiohttp/client.py", line 619, in _request
break
File "/path/to/my/venv/lib/python3.9/site-packages/aiohttp/helpers.py", line 656, in __exit__
raise asyncio.TimeoutError from None
asyncio.exceptions.TimeoutError
相关代码:
import discord
import asyncio
from quart import Quart, request, Response
app = Quart(__name__)
client = discord.Client()
@app.before_serving
async def before_serving():
loop = asyncio.get_event_loop()
await client.login(os.getenv('TOKEN'))
loop.create_task(client.connect())
@app.route('/webhook', methods=['POST'])
async def webhook():
...
await channel.send(content, embed=embed)
...
如何让客户端循环保持一天以上? 有没有办法防止客户端会话断开连接,或者我应该定期重新连接它?
我咳嗽 asyncio.TimeoutError 并设法重新启动会话:
async def discord_post(channel, message, embed):
try:
await channel.send(message, embed=embed)
except asyncio.TimeoutError:
await connect_client()
await channel.send(message, embed=embed)
@app.before_serving
async def connect_client():
loop = asyncio.get_event_loop()
await client.login(os.getenv('DISCORD_TOKEN'))
loop.create_task(client.connect())
@app.route('/webhook', methods=['POST'])
async def webhook():
...
await discord_post(channel, text, embed)
...
但是处理重启需要将近 300 秒,之后所有请求都会被立即处理并且 nginx 一直返回 499,即使代码现在正确执行,但我想这完全是另一个问题