Python_2_Examples_and_Notes/17x_threading_queue_non-cla...

27 lines
509 B
Python

#!/usr/bin/python
# -*- coding: utf-8 -*-
# Thanks: https://www.troyfawkes.com/learn-python-multithreading-queues-basics/
from Queue import Queue
from threading import Thread
def do_work(q):
while True:
print q.get()
q.task_done()
q = Queue(maxsize=0)
num_threads = 5
for i in range(num_threads):
worker = Thread(target=do_work, args=(q,))
worker.setDaemon(True)
worker.start()
for a in range (10):
for b in range(50):
q.put(a+b*50)
q.join()
print "Work " + str(y) + ": done."