File: class/Extras/Code/pp3e/queuetest.py
import thread, Queue, time
dataQueue = Queue.Queue() # infinite size
def producer(id):
for i in range(5):
#time.sleep(0.1)
dataQueue.put('producer %d:%d' % (id, i))
def consumer(id):
while 1:
time.sleep(0.1)
try:
data = dataQueue.get(block=False)
except Queue.Empty:
pass
else:
print 'consumer', id, 'got', data
for i in range(4):
thread.start_new_thread(consumer, (i,))
for i in range(4):
thread.start_new_thread(producer, (i,))
time.sleep(5)
##import threading
##for i in range(5):
## thread = Threading.Thread(target=lambda: consumer(i))
## thread.start()
##for i in range(5):
## thread = Threading.Thread(target=lambda: producer(i))
## thread.start()