Загрузка данных


import asyncio, aiohttp, time
URL = "https://bandit.cloudpub.ru/PROECT_ZDOH_GET_TORNADO"
TOTAL = 100000
CONCURRENT = 50

done = 0
err = 0
lock = asyncio.Lock()

async def worker(session, sem, req_id):
    global done, err
    try:
        async with sem:
            start = time.perf_counter()
            async with session.get(URL, timeout=aiohttp.ClientTimeout(total=10)) as resp:
                await resp.text()
                rt = (time.perf_counter() - start) * 1000
                async with lock:
                    done += 1
                    print(f"#{req_id} | {resp.status} | {rt:.2f}ms")
    except Exception as e:
        async with lock:
            err += 1
            done += 1
            print(f"#{req_id} | ERROR | {type(e).__name__}")

async def main():
    sem = asyncio.Semaphore(CONCURRENT)
    async with aiohttp.ClientSession(connector=aiohttp.TCPConnector(limit=0)) as sess:
        tasks = [asyncio.create_task(worker(sess, sem, i)) for i in range(TOTAL)]
        await asyncio.gather(*tasks)

if __name__ == "__main__":
    asyncio.run(main())