May 2001
Intermediate to advanced
304 pages
6h 12m
English
The Queue module provides a thread-safe queue implementation, shown in Example 3-2. It provides
a convenient way of moving Python objects between different threads.
Example 3-2. Using the Queue Module
File: queue-example-1.py
import threading
import Queue
import time, random
WORKERS = 2
class Worker(threading.Thread):
def _ _init_ _(self, queue):
self._ _queue = queue
threading.Thread._ _init_ _(self)
def run(self):
while 1:
item = self._ _queue.get()
if item is None:
break # reached end of queue
# pretend we're doing something that takes 10—100 ms
time.sleep(random.randint(10, 100) / 1000.0)
print "task", item, "finished"
#
# try it
queue = Queue.Queue(0)
for i in range(WORKERS):
Worker(queue).start() # start a worker
for i in range(10):
queue.put(i)
for i in range(WORKERS):
queue.put(None) # add end-of-queue markers
task 1 finished
task 0 finished
task 3 finished
task 2 finished
task 4 finished
task 5 finished
task 7 finished
task 6 finished
task 9 finished
task 8 finishedExample 3-3 shows how you can limit the size of the queue. If the producer threads fill the queue, they will block until items are popped off the queue.
Example 3-3. Using the Queue Module with a Maximum Size
File: queue-example-2.py import threading import Queue import time, random WORKERS = 2 class Worker(threading.Thread): def _ _init_ _(self, queue): self._ _queue = queue threading.Thread._ _init_ _(self) def run(self): while 1: item = self._ _queue.get() if item is None: break # reached end ...
Read now
Unlock full access