yanivk1984
9/21/2019 - 2:54 PM

python threading with Queue

import  threading
import time
from queue import Queue

print_lock = threading.Lock()

def worker(job):
    time.sleep(2)
    with print_lock:
        print(threading.current_thread().name, 'Task'+str(job))


def worker_thread():
    while True:
        job = q.get()  # block until there is an available worker in the queue
        worker(job)  # start the thread
        q.task_done()  # notify the Queue that the task was done in order to release it and get other jobs


q = Queue()
num_jobs = 10 # How much threads will run in total (total jobs)
num_workers = 10  # how much threads will run at once

for wk in range(num_workers):
    t = threading.Thread(target=worker_thread)  # call the worker
    t.name = 'Worker-'+str(wk)  # name the worker
    t.daemon = True  # kill the thread when the main thread is finished
    t.start()

start = time.time()

for job in range(num_jobs):
    q.put(job)  # in case that there is a worker available put it to work

q.join()

print('Entire job took:', time.time()-start)