Created
February 24, 2018 20:48
-
-
Save alexbrillant/4707c3877d9cc10adae4a0feb9e0656b to your computer and use it in GitHub Desktop.
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
from queue import Queue | |
from threading import Thread | |
class DownloadWorker(Thread): | |
def __init__(self, queue): | |
Thread.__init__(self) | |
self.queue = queue | |
def run(self): | |
while True: | |
# Get the work from the queue and expand the tuple | |
directory, link = self.queue.get() | |
download_link(directory, link) | |
self.queue.task_done() | |
def main(): | |
ts = time() | |
client_id = os.getenv('IMGUR_CLIENT_ID') | |
if not client_id: | |
raise Exception("Couldn't find IMGUR_CLIENT_ID environment variable!") | |
download_dir = setup_download_dir() | |
links = [l for l in get_links(client_id) if l.endswith('.jpg')] | |
# Create a queue to communicate with the worker threads | |
queue = Queue() | |
# Create 8 worker threads | |
for x in range(8): | |
worker = DownloadWorker(queue) | |
# Setting daemon to True will let the main thread exit even though the workers are blocking | |
worker.daemon = True | |
worker.start() | |
# Put the tasks into the queue as a tuple | |
for link in links: | |
logger.info('Queueing {}'.format(link)) | |
queue.put((download_dir, link)) | |
# Causes the main thread to wait for the queue to finish processing all the tasks | |
queue.join() | |
print('Took {}'.format(time() - ts)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment