Python多线程生产者消费者模式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
from Queue import Queue

import threading
import random
import time

class ProduceToQueue( threading.Thread ):
def __init__( self, threadName, queue ):
threading.Thread.__init__( self, name = threadName )
self.sharedObject = queue

def run( self ):
for i in range( 11, 21 ):
time.sleep( random.randrange( 4 ) )
print "%s adding %s to queue" % ( self.getName(), i )
self.sharedObject.put( i )

print self.getName(), "finished producing values"
print "Terminating", self.getName()

class ConsumeFromQueue( threading.Thread ):
def __init__( self, threadName, queue ):
threading.Thread.__init__( self, name = threadName )
self.sharedObject = queue

def run( self ):
sum = 0
current = 10

for i in range( 10 ):
time.sleep( random.randrange( 4 ) )
print "%s attempting to read %s..." % ( self.getName(), current + 1 )
current = self.sharedObject.get()
print "%s read %s" % ( self.getName(), current )
sum += current

print "%s retrieved values totaling: %d" % ( self.getName(), sum )
print "Terminating", self.getName()

queue = Queue()
producer = ProduceToQueue( "Producer", queue )
consumer = ConsumeFromQueue( "Consumer", queue )

producer.start()
consumer.start()

producer.join()
consumer.join()