Skip to content

Instantly share code, notes, and snippets.

@DataGreed
Created September 4, 2019 18:41
Show Gist options
  • Save DataGreed/922194d6a259bede82190d473be678fd to your computer and use it in GitHub Desktop.
Save DataGreed/922194d6a259bede82190d473be678fd to your computer and use it in GitHub Desktop.
# example of threads working on a queue
from Queue import Queue
from threading import Thread
num_worker_threads = 10
# items to work on
def source():
return xrange(400)
# the actual work to be done
def do_work(smth):
print("{}\r".format(smth))
# worker that retrieves data from queue and executes work
def worker():
while True:
item = q.get()
do_work(item)
q.task_done()
q = Queue()
for i in range(num_worker_threads):
t = Thread(target=worker) #use args to pass args
t.daemon = True
t.start()
# queues items to work on
for item in source():
q.put(item)
# blocks until work is finished
q.join()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment