Created
August 3, 2016 17:07
-
-
Save lamngo255/8870a7a4df448e0ce2ab8f3abb1d0fcd 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
import threading | |
from queue import Queue | |
import time | |
# 10 workers, 20 tasks | |
NUM_WORKERS = 10 | |
NUM_JOBS = 20 | |
start = time.time() | |
q = Queue() | |
def exampleJob(jobID): | |
time.sleep(0.5) | |
print(threading.currentThread().name, jobID) | |
def work(): | |
while True: | |
todoJob = q.get() | |
exampleJob(todoJob) | |
q.task_done() | |
def create_workers(): | |
for x in range(NUM_WORKERS): | |
t = threading.Thread(target=work) | |
t.daemon = True | |
t.start() | |
def create_jobs(): | |
for job in range(NUM_JOBS): | |
q.put(job) | |
q.join() | |
create_workers() | |
create_jobs() | |
print('Entire job took:', time.time() - start) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment