add concurrence examples

This commit is contained in:
Mia von Steinkirch 2020-03-21 14:24:55 -07:00
parent 614452d462
commit 516c922e02
15 changed files with 64 additions and 248 deletions

View file

@ -1,36 +1,19 @@
#!/usr/bin/env python3
import time
from queue import Queue
from threading import Thread
NUM_WORKERS = 4
task_queue = Queue()
def worker():
# Constantly check the queue for addresses
while True:
address = task_queue.get()
run_function(address)
# Mark the processed task as done
task_queue.task_done()
start_time = time.time()
# Create the worker threads
threads = [Thread(target=worker) for _ in range(NUM_WORKERS)]
# Add the websites to the task queue
[task_queue.put(item) for item in SOME_LIST]
# Start all the workers
[task_queue.put(item) for item in threads]
[thread.start() for thread in threads]
# Wait for all the tasks in the queue to be processed
task_queue.join()
end_time = time.time()
print('Time: {} secs'.format(end_time - start_time))
task_queue.join()