A Better Coroutine Example

Let's see a good example of coroutine using aiohttp package.

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:

Press + to interact
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 ...