In the following example, we will import the modules that we need and then create our first coroutine using the async syntax. This coroutine is called download_file, and it uses Python's requests module to download whatever file is passed to it. When it is done, it will return a message that's related to the file that is being downloaded.
You can find the following code in the download_files_asyncio.py file:
#!/usr/bin/python3
import asyncio
import os
import requests
import time
files = ['https://docs.python.org/3/archives/python-3.7.2-docs-pdf-letter.zip', 'https://docs.python.org/3/archives/python-3.7.2-docs-pdf-a4.zip']
async def download_file(url):
response = requests.get(url)
filename = os.path.basename(url)
print('Downloading {filename}'.format(filename=filename))
open(filename, 'wb').write(response.content)
msg = 'Finished downloading {filename}'.format(filename=filename)
return msg
In the previous code block, we defined our file list for downloading and the download_file() method, which accepts the URL that contains the file as a parameter. In the following code block, we are defining the main function that we are going to use for downloading files in an asynchronous way. We will do this by using coroutines with the async-await mechanism:
async def main(files):
coroutines = [download_file(file) for file in files]
completed, pending = await asyncio.wait(coroutines)
for item in completed:
print(item.result())
if __name__ == '__main__':
t1 = time.time()
event_loop = asyncio.get_event_loop()
try:
event_loop.run_until_complete(main(files))
finally:
event_loop.close()
print(time.time() - t1, 'seconds passed')
This is the output of this script's execution:
Downloading python-3.7.2-docs-pdf-a4.zip
Downloading python-3.7.2-docs-pdf-letter.zip
Finished downloading python-3.7.2-docs-pdf-letter.zip
Finished downloading python-3.7.2-docs-pdf-a4.zip
11.149724960327148 seconds passed
In this execution, we can see the files to be downloaded, as well as the execution time for downloading these files.