Asyncio 和 Discord.py 超时上下文管理器应该在任务中使用

Posted

技术标签:

【中文标题】Asyncio 和 Discord.py 超时上下文管理器应该在任务中使用【英文标题】:Asyncio and Discord.py Timeout context manager should be used inside a task 【发布时间】:2021-05-18 05:28:47 【问题描述】:

我正在为不和谐的女巫使用 asynco 制作一个 rule34 机器人我正在使用 getImages() 函数,如果 r34 等于 rule34.Rule34(my current loop) 则应该可以使用

我的代码

import discord
import rule34
import nest_asyncio

nest_asyncio.apply()
client = discord.Client()

class bot:
    def __init__(self, message):
        client = discord.Client()
        self.r34 = rule34.Rule34(client.loop)
        self.message = message

    async def send(self, message): #retrives and sends the image
        post = await self.r34.getImages(singlePage=True, OverridePID=1, tags=message.content.split()[1:]) #retrives rule34 object
        print(post)


@client.event
async def on_message(message):
    if message.content.startswith("?p"): #if it's requesting rule34
        post = bot(message)
        await post.send(message)

client.run('MY TOKEN')

输出

Ignoring exception in on_message
Traceback (most recent call last):
  File "/home/lj_fin/.local/lib/python3.6/site-packages/discord/client.py", line 343, in _run_event
    await coro(*args, **kwargs)
  File "schmeckle.py", line 29, in on_message
    await post.send(message)
  File "schmeckle.py", line 17, in send
    post = await self.r34.getImages(singlePage=True, OverridePID=1, tags=message.content.split()[1:]) #retrives rule34 object
  File "/home/lj_fin/.local/lib/python3.6/site-packages/rule34/rule34.py", line 161, in getImages
    num = await self.totalImages(tags)
  File "/home/lj_fin/.local/lib/python3.6/site-packages/rule34/rule34.py", line 131, in totalImages
    with async_timeout.timeout(10):
  File "/home/lj_fin/.local/lib/python3.6/site-packages/async_timeout/__init__.py", line 39, in __enter__
    return self._do_enter()
  File "/home/lj_fin/.local/lib/python3.6/site-packages/async_timeout/__init__.py", line 76, in _do_enter
    raise RuntimeError('Timeout context manager should be used '
RuntimeError: Timeout context manager should be used inside a task

我取消程序时的输出

Error in atexit._run_exitfuncs:
Traceback (most recent call last):
  File "/home/lj_fin/.local/lib/python3.6/site-packages/rule34/rule34.py", line 53, in _exitHandler
    asyncio.run_coroutine_threadsafe(self.session.close(), loop=self.loop)
  File "/usr/lib/python3.6/asyncio/tasks.py", line 725, in run_coroutine_threadsafe
    loop.call_soon_threadsafe(callback)
  File "/usr/lib/python3.6/asyncio/base_events.py", line 637, in call_soon_threadsafe
    self._check_closed()
  File "/usr/lib/python3.6/asyncio/base_events.py", line 377, in _check_closed
    raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed
Error in atexit._run_exitfuncs:
Traceback (most recent call last):
  File "/home/lj_fin/.local/lib/python3.6/site-packages/rule34/rule34.py", line 53, in _exitHandler
    asyncio.run_coroutine_threadsafe(self.session.close(), loop=self.loop)
  File "/usr/lib/python3.6/asyncio/tasks.py", line 725, in run_coroutine_threadsafe
    loop.call_soon_threadsafe(callback)
  File "/usr/lib/python3.6/asyncio/base_events.py", line 637, in call_soon_threadsafe
    self._check_closed()
  File "/usr/lib/python3.6/asyncio/base_events.py", line 377, in _check_closed
    raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed
Unclosed client session
client_session: <aiohttp.client.ClientSession object at 0x7effb8ac87b8>
sys:1: RuntimeWarning: coroutine 'ClientSession.close' was never awaited
Unclosed client session
client_session: <aiohttp.client.ClientSession object at 0x7effb38f5518>

【问题讨论】:

【参考方案1】:

客户不应该是commands.Bot 而不是discord.Client

编辑:我知道这不是“答案”,但我没有足够的代表来添加评论。

【讨论】:

no commands.Bot 用于为 bot discord 设置命令。Client 是网络中使用的东西 哦。不知道

以上是关于Asyncio 和 Discord.py 超时上下文管理器应该在任务中使用的主要内容,如果未能解决你的问题,请参考以下文章

C-Python asyncio:在线程中运行 discord.py

Python asyncio/discord.py - 循环退出,任务被破坏,但它处于待处理状态

在python中同时运行多个协程[Discord.py]

Discord.py - 从错误对象中获取上下文

多场景用户输入 Discord.py

如果达到 Wait_For_Message 超时,则发送消息 Discord Py