Search⌘ K

A Better Coroutine Example

Explore how to implement asynchronous file downloads in Python using the aiohttp package. Learn to create client sessions, manage coroutines, handle streaming responses, and properly release connections within an async event loop to optimize network and IO tasks.

We'll cover the following...

The aiohttp package is designed for creating asynchronous HTTP clients and servers. You can install it with pip like this:

Javascript (babel-node)
pip3 install aiohttp

Using aiohttp to download files

Once that’s installed, let’s update our code to use aiohttp so that we can download the files:

Note: Click on the link below the widget to see the list of downloaded pdf(s) opened.

import aiohttp
import asyncio
import async_timeout
import os

import time

async def download_coroutine(session, url):
    with async_timeout.timeout(1000):
        async with session.get(url) as response:
            filename = os.path.basename(url)
            with open(filename, 'wb') as f_handle:
                while True:
                    chunk = await response.content.read(1024)
                    if not chunk:
                        break
                    f_handle.write(chunk)
                await response.release()
                return response.status == 200

async def main(loop):
    urls = ["http://www.irs.gov/pub/irs-pdf/f1040.pdf",
        "http://www.irs.gov/pub/irs-pdf/f1040a.pdf",
        "http://www.irs.gov/pub/irs-pdf/f1040ez.pdf",
        "http://www.irs.gov/pub/irs-pdf/f1040es.pdf",
        "http://www.irs.gov/pub/irs-pdf/f1040sb.pdf"]

    async with aiohttp.ClientSession(loop=loop) as session:
        for url in urls:
            print(await download_coroutine(session, url))
            


if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    loop.run_until_complete(main(loop))
Using aiohttp to download the files

You ...