Skip to content

Instantly share code, notes, and snippets.

@KarimullinArthur
Created January 19, 2024 11:26
Show Gist options
  • Select an option

  • Save KarimullinArthur/2170efa7ee8f8500b58d6001c9e41363 to your computer and use it in GitHub Desktop.

Select an option

Save KarimullinArthur/2170efa7ee8f8500b58d6001c9e41363 to your computer and use it in GitHub Desktop.

Revisions

  1. KarimullinArthur created this gist Jan 19, 2024.
    52 changes: 52 additions & 0 deletions aiohttp_vs_requests.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,52 @@
    import aiohttp
    import asyncio
    import requests
    import time


    async def aio():
    URL = 'https://cleanuri.com/api/v1/shorten'
    links = ['https://artlin.codeberg.page/contacts.html'] * 10

    async with aiohttp.ClientSession() as session:
    tasks = []
    for link in links:
    data = {'url': link}
    tasks.append(session.post(url=URL, data=data))

    result = await asyncio.gather(*tasks)

    return result


    def req():
    URL = 'https://cleanuri.com/api/v1/shorten'

    links = ['https://artlin.codeberg.page/contacts.html'] * 10

    result = []

    for link in links:
    data = {'url': link}
    result.append(requests.post(url=URL, data=data))

    return result


    print('AioHTTP')
    for i in range(3):
    start_time = time.time()
    asyncio.run(aio())
    end_time = time.time()

    elapsed_time = end_time - start_time
    print(f"Elapsed time: {elapsed_time}")

    print('\nRequests')
    for i in range(3):
    start_time = time.time()
    req()
    end_time = time.time()

    elapsed_time = end_time - start_time
    print(f"Elapsed time: {elapsed_time}")