asyncio + aiohttp Получаю ошибку - RuntimeError: Event loop is closed

Kwuuuwh

Новичок
Пользователь
Авг 1, 2022
11
0
1
Здраствуйте, недавно начал писать асинхронный парсер для cs money (только учусь программированию) и столкнулся с следующей ошибкой :

Код:
self._context.run(self._callback, *self._args)
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
<coroutine object ClientResponse.text at 0x000001B5F9DAAF80>
0.8101880550384521
Exception ignored in: <function _ProactorBasePipeTransport.__del__ at 0x000001B5F95232E0>
Traceback (most recent call last):
  File "C:\Users\Kwuuuwh\AppData\Local\Programs\Python\Python310\lib\asyncio\proactor_events.py", line 116, in __del__
    self.close()
  File "C:\Users\Kwuuuwh\AppData\Local\Programs\Python\Python310\lib\asyncio\proactor_events.py", line 108, in close
    self._loop.call_soon(self._call_connection_lost, None)
  File "C:\Users\Kwuuuwh\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py", line 750, in call_soon 
    self._check_closed()
  File "C:\Users\Kwuuuwh\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py", line 515, in _check_closed
    raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed

Собственно сам код:

Python:
from fake_useragent import UserAgent
import random
import asyncio
import aiohttp
import time
import json

data = []

async def get_html_data(url, session):
    headers = {"user-agent": UserAgent().random}

    async with session.get(url=url, headers=headers) as resp:
        response = resp.text()
        print(response)

async def create_task_html():
    async with aiohttp.ClientSession() as session:
        tasks = []

        for offset in range(0, 2):
            url = f'https://inventories.cs.money/5.0/load_bots_inventory/730?limit=32&offset={offset}&order=desc&priceWithBonus=30&sort=price&withStack=true'
            task = asyncio.create_task(get_html_data(url, session))
            tasks.append(task)

        await asyncio.gather(*tasks)

def main():
    start_time = time.time()

    asyncio.run(create_task_html())

    finish_time = time.time() - start_time
    print(finish_time)

if __name__ == '__main__':
    main()

Пытался решить проблему путём добавления в функцию main() за место asyncio.run(create_task_html()) следующую команду:
asyncio.get_event_loop().run_until_complete(create_task_html()) ,но в итоге в вывод идёт сначала:
Код:
<html>
<head><title>429 Too Many Requests</title></head>
<body>
<center><h1>429 Too Many Requests</h1></center>
<hr><center>nginx</center>
</body>
</html>
<!-- a padding to disable MSIE and Chrome friendly error page -->
<!-- a padding to disable MSIE and Chrome friendly error page -->
<!-- a padding to disable MSIE and Chrome friendly error page -->
<!-- a padding to disable MSIE and Chrome friendly error page -->
<!-- a padding to disable MSIE and Chrome friendly error page -->
<!-- a padding to disable MSIE and Chrome friendly error page -->

<html>
<head><title>429 Too Many Requests</title></head>
<body>
<center><h1>429 Too Many Requests</h1></center>
<hr><center>nginx</center>
</body>
</html>
<!-- a padding to disable MSIE and Chrome friendly error page -->
<!-- a padding to disable MSIE and Chrome friendly error page -->
<!-- a padding to disable MSIE and Chrome friendly error page -->
<!-- a padding to disable MSIE and Chrome friendly error page -->
<!-- a padding to disable MSIE and Chrome friendly error page -->
<!-- a padding to disable MSIE and Chrome friendly error page -->

после желаемый мне вывод и в заключении ошибка из начала

windows 10 pro
python - 3.10.5
aiohttp 3.7.4.post0
asyncio 3.4.3
 

Форум IT Специалистов