Skip to content

Instantly share code, notes, and snippets.

@btoo
Created December 17, 2017 03:50
Show Gist options
  • Save btoo/5b56ffbb88189751747b4dfce4c204a0 to your computer and use it in GitHub Desktop.
Save btoo/5b56ffbb88189751747b4dfce4c204a0 to your computer and use it in GitHub Desktop.
python-multithreading-demo
import threading
from queue import Queue
import time
print_lock = threading.Lock() # special lock we're using to make sure each worker won't pring until it's done with it job
def exampleJob(worker):
time.sleep(0.5) # let's say a job takes .5s to finish
with print_lock: # with the lock, print some info. when that's done, release the lock so another worker can print with it
print(threading.current_thread().name, worker)
def threader():
while True:
worker = q.get() # get a worker (thread)
exampleJob(worker) # run a job with the worker
q.task_done() # when the job is done, say so - this will release the thread/worker/daemon back into the queue for availability
q = Queue()
for x in range(10): # we have 10 workers to do our jobs
t = threading.Thread(target = threader)
t.daemon = True # when the main thread dies, all of the daemons will die with it
t.start()
start = time.time() # lets see how much time it takes with a linear program vs one with multiple threads in parallel
for worker in range(20): # we have 20 jobs that we want to do
q.put(worker) # literally putting a worker to work in the queue
q.join() # wait until threads terminate
print('Entire job took:', time.time() - start) # whole program should run for about 1 second because we have 10 workers working on 20 jobs that each take .5s each
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment