@cdph

Откуда возникает ошибка “RuntimeError: Event loop is closed”?

Совершенно не понимаю откуда берётся ошибка и что нужно сделать для её решения

import os
import asyncio

from aiohttp import ClientSession


class VkApi:
    def __init__(self, token: str, api_version: str = '5.131') -> None:
        self.__session = asyncio.run(self.get_session())
        self.api_version = api_version
        self.token = token

    async def get_session(self) -> ClientSession:
        return ClientSession()

    async def get_user(self,
                       id: str,
                       fields: list | tuple = None,
                       name_case: str = 'nom'
                    ):

        if fields:
            fields = ','.join(fields)

        data = await self.__execute_method('users.get',
                                            user_id=id,
                                            name_case=name_case,
                                            fields=fields
                                        )

        return data 

    async def __execute_method(self, method: str, **params) -> dict:
        params = params | {'access_token': self.token, 'v': self.api_version}

        async with self.__session.get(url=f'https://api.vk.com/method/{method}/', params=params) as resp:
            data = await resp.json()

        return data   


from dotenv import load_dotenv
load_dotenv()

token = os.getenv('VK_TOKEN')
vk_session = VkApi(token=token)

ioloop = asyncio.new_event_loop()

task = ioloop.create_task(vk_session.get_user(id=1, name_case='nom', fields=['bdate', 'books']))
ioloop.run_until_complete(task)


На что получаю:
Traceback (most recent call last):
File "/home/egor/python/vk_tg_bot/vk/vk_api.py", line 52, in
ioloop.run_until_complete(task)
File "/usr/lib/python3.10/asyncio/base_events.py", line 646, in run_until_complete
return future.result()
File "/home/egor/python/vk_tg_bot/vk/vk_api.py", line 25, in get_user
data = await self.__execute_method('users.get',
File "/home/egor/python/vk_tg_bot/vk/vk_api.py", line 36, in __execute_method
async with self.__session.get(url=f'https://api.vk.com/method/{method}/', params=params) as resp:
File "/home/egor/python/vk_tg_bot/venv/lib/python3.10/site-packages/aiohttp/client.py", line 1141, in __aenter__
self._resp = await self._coro
File "/home/egor/python/vk_tg_bot/venv/lib/python3.10/site-packages/aiohttp/client.py", line 448, in _request
handle = tm.start()
File "/home/egor/python/vk_tg_bot/venv/lib/python3.10/site-packages/aiohttp/helpers.py", line 651, in start
return self._loop.call_at(when, self.__call__)
File "/usr/lib/python3.10/asyncio/base_events.py", line 729, in call_at
self._check_closed()
File "/usr/lib/python3.10/asyncio/base_events.py", line 515, in _check_closed
raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed
Unclosed client session
client_session:
(venv) egor@egor-ThinkPad-T440:~/python/vk_tg_bot$ /home/egor/python/vk_tg_bot/venv/bin/python /home/egor/python/vk_tg_bot/vk/vk_api.pyTraceback (most recent call last):
File "/home/egor/python/vk_tg_bot/vk/vk_api.py", line 51, in
ioloop.run_until_complete(task)
File "/usr/lib/python3.10/asyncio/base_events.py", line 646, in run_until_complete
return future.result()
File "/home/egor/python/vk_tg_bot/vk/vk_api.py", line 25, in get_user
data = await self.__execute_method('users.get',
File "/home/egor/python/vk_tg_bot/vk/vk_api.py", line 36, in __execute_method
async with self.__session.get(url=f'https://api.vk.com/method/{method}/', params=params) as resp:
File "/home/egor/python/vk_tg_bot/venv/lib/python3.10/site-packages/aiohttp/client.py", line 1141, in __aenter__
self._resp = await self._coro
File "/home/egor/python/vk_tg_bot/venv/lib/python3.10/site-packages/aiohttp/client.py", line 448, in _request
handle = tm.start()
File "/home/egor/python/vk_tg_bot/venv/lib/python3.10/site-packages/aiohttp/helpers.py", line 651, in start
return self._loop.call_at(when, self.__call__)
File "/usr/lib/python3.10/asyncio/base_events.py", line 729, in call_at
self._check_closed()
File "/usr/lib/python3.10/asyncio/base_events.py", line 515, in _check_closed
raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed
Unclosed client session
client_session:
  • Вопрос задан
  • 176 просмотров
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы