-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implements file download and conversion in parallel using context man…
…ager
- Loading branch information
1 parent
9da715e
commit ee3cb2e
Showing
4 changed files
with
97 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,35 @@ | ||
|
||
import logging | ||
import urllib.request | ||
from urllib.parse import urlparse | ||
import tempfile | ||
|
||
from urllib.parse import urlparse | ||
from urllib.request import urlretrieve | ||
from concurrent.futures import ThreadPoolExecutor | ||
from concurrent.futures import as_completed | ||
from contextlib import contextmanager | ||
from typing import List | ||
from dataclasses import dataclass | ||
|
||
def download_file(signed_url: str): | ||
logging.info(f"Downloading input: {urlparse(signed_url).path}") | ||
file_name, headers = urllib.request.urlretrieve(signed_url) | ||
def download_file(url: str): | ||
logging.info(f"Downloading input: {urlparse(url).path}") | ||
file_name, _ = urlretrieve(url) | ||
logging.info(f"Downloaded input") | ||
return file_name | ||
|
||
@contextmanager | ||
def download_files_manager(url_list: List[str]): | ||
try: | ||
thread_list = [] | ||
list_objects = [] | ||
for url in url_list: | ||
filename = tempfile.NamedTemporaryFile() | ||
logging.info("Downloading file from url -> %s, filename -> %s", url, filename.name) | ||
with ThreadPoolExecutor(max_workers=20) as exe: | ||
thread_list.append(exe.submit(urlretrieve, url, filename.name)) | ||
list_objects.append(filename) | ||
for thread in as_completed(thread_list): | ||
path, _ = thread.result() | ||
logging.info("File downloaded to: %s", path) | ||
yield [obj.name for obj in list_objects] | ||
finally: | ||
for obj in list_objects: | ||
obj.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters