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
15 changes: 15 additions & 0 deletions get_files.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
"""This module exports a function that gets a list of files"""
import os


def get_files_list(url='http://ftp.mgts.by/test/'):
"""Creates a list of links

This function creates a list of links to files
for downloading
"""

url = 'http://ftp.mgts.by/test/'
files = ("10Mb.txt", "50Mb.txt", "100Mb.txt")

return [os.path.join(url, f) for f in files]
46 changes: 46 additions & 0 deletions homework5_async.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
"""Homework 5: Downloading multiple files using async"""
import aiohttp
import asyncio
from get_files import get_files_list
import os
import sys
import time


async def get_url(url, path, session, sem_number):
name = url.split('/')[-1]
filename = os.path.join(path, name)
sem = asyncio.Semaphore(sem_number)
async with sem:
async with session.get(url) as response:
with open(filename, 'wb') as fd:
print("Updating {}".format(filename))
async for data in response.content.iter_chunked(4096):
fd.write(data)
return 'Successfully downloaded ' + filename


async def main(urls, path, sem_number):
async with aiohttp.ClientSession() as session:
tasks = [get_url(url, path, session, sem_number) for url in urls]
return await asyncio.gather(*tasks)

if __name__ == '__main__':
links = get_files_list()

sem_number = 0
if len(sys.argv) == 2:
try:
sem_number = int(sys.argv[1])
except Exception:
print("Wrong type of argument")
sys.exit(1)
else:
sem_number = 2

start_time = time.time()
loop = asyncio.get_event_loop()
result = loop.run_until_complete(main(links, '/tmp', sem_number))
loop.close()
print("\n".join(result))
print("Final time: {:.2f}".format(time.time() - start_time))
54 changes: 54 additions & 0 deletions homework5_threads.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
"""Homework 5: Downloading multiple files using threads"""
from get_files import get_files_list
import os
from queue import Queue
import threading
import time
import urllib.request


class DownloadThread(threading.Thread):
"""Implementation of a thread to download"""

def __init__(self, queue, dest):
super().__init__()
self.queue = queue
self.dest = dest
self.daemon = True

def run(self):
"Implementation of thread's activity"
while True:
url = self.queue.get()
try:
self.download_url(url)
except Exception as e:
print("Error: {}".format(e))
self.queue.task_done()

def download_url(self, url):
"Download a file from an URL"
name = url.split('/')[-1]
destfile = os.path.join(self.dest, name)
print("downloading {} -> {}".format(url, destfile))
urllib.request.urlretrieve(url, destfile)


def download(urls, destfolder, numthreads=2):
"""Threads master that controls downloading of all files"""
queue = Queue()
for url in urls:
queue.put(url)

for i in range(numthreads):
t = DownloadThread(queue, destfolder)
t.start()

queue.join()


if __name__ == '__main__':
files = get_files_list()
start_time = time.time()
download(files, '/tmp')
print("Final time: {:.2f}".format((time.time() - start_time)))
19 changes: 17 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,17 @@
flake8
pydocstyle
aiohttp==3.5.4
async-timeout==3.0.1
asyncio==3.4.3
attrs==18.2.0
chardet==3.0.4
flake8==3.6.0
idna==2.8
idna-ssl==1.1.0
mccabe==0.6.1
multidict==4.5.2
pycodestyle==2.4.0
pydocstyle==3.0.0
pyflakes==2.0.0
six==1.12.0
snowballstemmer==1.2.1
typing-extensions==3.7.2
yarl==1.3.0