Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added hw5/__init__.py
Empty file.
49 changes: 49 additions & 0 deletions hw5/async_download.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
"""Homework5: download a bunch of files using 'asyncio', 'aiohttp'"""

import aiohttp
import asyncio
import os
import time

urls = ['https://www.python.org/ftp/python/3.8.0/Python-3.8.0a2.tgz',
'https://www.python.org/ftp/python/2.7.16/Python-2.7.16rc1.tgz',
'https://www.python.org/ftp/python/3.8.0/Python-3.8.0a1.tgz',
'https://www.python.org/ftp/python/3.7.2/Python-3.7.2rc1.tgz',
'https://www.python.org/ftp/python/3.6.8/Python-3.6.8rc1.tgz',
'https://www.python.org/ftp/python/3.7.1/Python-3.7.1rc2.tgz',
'https://www.python.org/ftp/python/3.6.7/Python-3.6.7rc2.tgz']

tasks = []


async def download(session, url):
"""Get a response from the url. Write its content to the file.

:param session: a client session
:param url: link to download
"""
async with session.get(url) as response:
content = await response.read()

with open("Asyncio_{}".format(os.path.basename(url)), 'wb') as file:
file.write(content)


async def start():
"""Create a session and run awaitable objects concurrently"""
async with aiohttp.ClientSession() as session:

for url in urls:
tasks.append(download(session, url))

await asyncio.gather(*tasks)


if __name__ == "__main__":

start_time = time.time()
asyncio.run(start())
finish_time = time.time()

print("Asyncio, aiohttp. Result time: {}".
format(finish_time - start_time))
48 changes: 48 additions & 0 deletions hw5/threads_download.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
"""Homework5: download a bunch of files using common threads"""

import os
import requests
import threading
import time

urls = ['https://www.python.org/ftp/python/3.8.0/Python-3.8.0a2.tgz',
'https://www.python.org/ftp/python/2.7.16/Python-2.7.16rc1.tgz',
'https://www.python.org/ftp/python/3.8.0/Python-3.8.0a1.tgz',
'https://www.python.org/ftp/python/3.7.2/Python-3.7.2rc1.tgz',
'https://www.python.org/ftp/python/3.6.8/Python-3.6.8rc1.tgz',
'https://www.python.org/ftp/python/3.7.1/Python-3.7.1rc2.tgz',
'https://www.python.org/ftp/python/3.6.7/Python-3.6.7rc2.tgz']

threads = []


def download(url):
"""Get a response from the url. Write its content to the file.

:param url: link to download
"""
target_file = "_".join([thread.name, os.path.basename(url)])

response = requests.get(url=url)

with open(target_file, "wb") as file:
file.write(response.content)


if __name__ == "__main__":

start_time = time.time()

for url in urls:
threads.append(threading.Thread(target=download, args=(url, )))

for thread in threads:
thread.start()

for thread in threads:
thread.join()

finish_time = time.time()

print("Common threads. Result time: {}".
format(finish_time - start_time))