0

I try using aiohttp for sending some request in loop. When I run this code I see in process list threads at first iteration: 5 thread, next iteration after 5 sec add 5 thread, etc

I try find answer, but I only found info about aiodns, I test with aiodns, but situations not chaged. aiohttp run thread, why ? what's I doing wrong ?

#!/usr/bin/python3
# -*- coding: utf-8 -*-

import asyncio
import aiohttp
import aiodns
import aiohttp.resolver
aiohttp.resolver.DefaultResolver = aiohttp.resolver.AsyncResolver

url = "http://localhost/script"

async def process_send():
    print("Run process_send")
    while 1:
        print(aiohttp.resolver.DefaultResolver)
        async with aiohttp.ClientSession() as session:
            async with session.post(url, data={'id': '13459393', 'data': 'somedate'}, timeout=15) as resp:
                print("Send result %s " % (str(resp.status)))
        await asyncio.sleep(5)

if __name__ == '__main__':
    ioloop = asyncio.get_event_loop()
    tasks = [ioloop.create_task(process_send()) for _ in range(5)]
    wait_tasks = asyncio.wait(tasks)
    ioloop.run_until_complete(wait_tasks)
    ioloop.close()

Evgenii G.
  • 45
  • 2
  • 8
  • I found recommendation about ClientSession(), and if I create session session = aiohttp.ClientSession() before while 1, aiohttp create 5 thread. But question still waiting, why it create thread? – Evgenii G. Jun 26 '20 at 07:10
  • I found problem, it was resolver... I create AsyncResolver, pass it to TCPConnecction, that's all. – Evgenii G. Jun 26 '20 at 08:41

1 Answers1

0

Only resolver.py can create thread.

from aiohttp.resolver import AsyncResolver

resolver = AsyncResolver()
tcp_conn = aiohttp.TCPConnector(resolver=resolver)
async with aiohttp.ClientSession(connector=tcp_conn) as session:
Evgenii G.
  • 45
  • 2
  • 8