example of threading
import threading
from queue import Queue
import time
print_lock = threading.Lock() # creates the print_lock object
def exampleJob(job):
time.sleep(0.5)
with print_lock: # check out the print lock to print
print(threading.current_thread().name, job)
def threader():
while True: # inifinite loop that only stops when queue is empty
job = q.get() # get a job
exampleJob(job) # run the method using the obtained job
q.task_done() # complete the job
q = Queue() # create the queue
for x in range(10): # 10 threads/workers
t = threading.Thread(target=threader) # create a thread that is defined by threader
t.daemon = True # make the thread a daemon process
t.start() # initiate the thread
start = time.time()
for job in range(20): # add 20 jobs to the job queue
q.put(job)
q.join() # stops the main thread from proceding
print('Entire job took: ',time.time() - start)