Skip to content

Instantly share code, notes, and snippets.

@aalllq
Forked from Den1al/aiohttp-example.py
Created February 9, 2022 15:00
Show Gist options
  • Select an option

  • Save aalllq/db498d6a69ecdf0fe3097ba8e7240cc1 to your computer and use it in GitHub Desktop.

Select an option

Save aalllq/db498d6a69ecdf0fe3097ba8e7240cc1 to your computer and use it in GitHub Desktop.

Revisions

  1. @Den1al Den1al renamed this gist Feb 13, 2018. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. @Den1al Den1al revised this gist Dec 20, 2017. 1 changed file with 8 additions and 4 deletions.
    12 changes: 8 additions & 4 deletions aiohttp_example.py
    Original file line number Diff line number Diff line change
    @@ -10,8 +10,11 @@
    async def fetch_all(urls: list):
    """ Fetch all URLs """
    tasks = []
    custom_headers = {
    'User-Agent': 'aiohttp client 0.17'
    }

    async with ClientSession() as session:
    async with ClientSession(headers=custom_headers) as session:
    for url in urls:
    task = asyncio.ensure_future(fetch(url, session))
    tasks.append(task)
    @@ -42,16 +45,17 @@ def main():
    urls = [
    'https://twitter.com',
    'https://9to5mac.com',
    'https://amazon.com',
    ] * 10
    'https://amazon.com'
    ]

    loop = asyncio.get_event_loop()
    total_future = asyncio.ensure_future(fetch_all(urls))

    loop.run_until_complete(total_future)

    total_elapsed = default_timer() - start_time
    print(f'{" total time of".rjust(28, "-")}: {total_elapsed:5.2f}')


    if __name__ == "__main__":
    main()
    main()
  3. @Den1al Den1al revised this gist Dec 20, 2017. No changes.
  4. @Den1al Den1al created this gist Dec 19, 2017.
    57 changes: 57 additions & 0 deletions aiohttp_example.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,57 @@
    # author: @Daniel_Abeles
    # date: 18/12/2017

    import asyncio
    from aiohttp import ClientSession
    from timeit import default_timer
    import async_timeout


    async def fetch_all(urls: list):
    """ Fetch all URLs """
    tasks = []

    async with ClientSession() as session:
    for url in urls:
    task = asyncio.ensure_future(fetch(url, session))
    tasks.append(task)

    _ = await asyncio.gather(*tasks)


    async def fetch(url: str, session: object):
    """ Fetch a single URL """
    with async_timeout.timeout(10):
    async with session.get(url) as response:
    before_request = default_timer()
    resp = await response.read()
    elapsed = default_timer() - before_request

    print(f'{url:30}{elapsed:5.2f}')

    return {
    'resp': resp,
    'url': url,
    'elapsed': elapsed
    }


    def main():
    """ Main Function """
    start_time = default_timer()
    urls = [
    'https://twitter.com',
    'https://9to5mac.com',
    'https://amazon.com',
    ] * 10

    loop = asyncio.get_event_loop()
    total_future = asyncio.ensure_future(fetch_all(urls))
    loop.run_until_complete(total_future)

    total_elapsed = default_timer() - start_time
    print(f'{" total time of".rjust(28, "-")}: {total_elapsed:5.2f}')


    if __name__ == "__main__":
    main()