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

2024-03-29 10:43:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在为discord witch创建一个规则34 bot,它使用asynco,我正在使用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>

Tags: runinpyselfclientloopasynciomessage