生产者-消费者模型是多线程同步的经典案例
此模型中生产者向缓冲区push数据,消费者从缓冲区中pull数据
这个Demo中缓冲区用python实现的Queue来做,这个模块是线程安全的使我不用再为队列增加额外的互斥锁.此外这个Demo中信号处理的实现是这样的:
1)主线程接到一个SIGTERM的信号后先通知Consumer停止向缓冲区push数据并退出
2)Produer将缓冲区中的数据消费完全后在退出
3)主线程退出
下面是部分代码,全部代码在github上面
class Consumer(threading.Thread): def __init__(self, queue): threading.Thread.__init__(self) self.queue = queue self.do = True def stop(self): self.do = False print 'change consumer.do to False' def run(self): print 'Create new consumer thread, id: %s' % self.ident while self.do: messages = [] result = [] msg = random.randint(0,100) self.queue.put(msg) print 'Consumer thread will exit.' class Producer(threading.Thread): def __init__(self, queue): threading.Thread.__init__(self) self.queue = queue self.msgs = Queue.Queue() self.state = State.NORMAL self.do = True def stop(self): self.do = False self.state = State.STOP def run(self): while self.do: if self.state == State.NORMAL: if not self.queue.empty(): data = self.queue.get() print 'Producer get data: %s' % data else: print 'data queue is empty, sleep 5 seconds.' time.sleep(5) elif self.state == State.STOP: while not self.queue.empty(): data = self.queue.get() print 'Producer get data: %s' % data print 'Producer thread will exit.'