之前用C++写过一篇生产者消费者的实现。

生产者和消费者主要是处理互斥和同步的问题:

队列作为缓冲区,需要互斥操作

队列中没有产品,消费者需要等待,直到生产者放入产品并通知它。队列慢的情况类似。

这里我使用list模拟Python标准库的Queue,这里我设置一个大小限制为5:

SyncQueue.py

from threading import Lock
from threading import Condition
class Queue():
def __init__(self):
self.mutex = Lock()
self.full = Condition(self.mutex)
self.empty = Condition(self.mutex)
self.data = [] def push(self, element):
self.mutex.acquire()
while len(self.data) >= 5:
self.empty.wait() self.data.append(element)
self.full.notify()
self.mutex.release() def pop(self):
self.mutex.acquire()
while len(self.data) == 0:
self.full.wait()
data = self.data[0]
self.data.pop(0)
self.empty.notify()
self.mutex.release() return data if __name__ == '__main__':
q = Queue()
q.push(10)
q.push(2)
q.push(13) print q.pop()
print q.pop()
print q.pop()

这是最核心的代码,注意里面判断条件要使用while循环。

接下来是生产者进程,producer.py

from threading import Thread
from random import randrange
from time import sleep
from SyncQueue import Queue class ProducerThread(Thread):
def __init__(self, queue):
Thread.__init__(self)
self.queue = queue
def run(self):
while True:
data = randrange(0, 100)
self.queue.push(data)
print 'push %d' % (data)
sleep(1) if __name__ == '__main__':
q = Queue()
t = ProducerThread(q)
t.start()
t.join()

消费者,Condumer.py

from threading import Thread
from time import sleep
from SyncQueue import Queue class ConsumerThread(Thread):
def __init__(self, queue):
Thread.__init__(self)
self.queue = queue
def run(self):
while True:
data = self.queue.pop()
print 'pop %d' % (data)
sleep(1) if __name__ == '__main__':
q = Queue()
t = ConsumerThread(q)
t.start()
t.join()

最后我们写一个车间类,可以指定线程数量:

from SyncQueue import Queue
from Producer import ProducerThread
from Consumer import ConsumerThread class WorkShop():
def __init__(self, producerNums, consumerNums):
self.producers = []
self.consumers = []
self.queue = Queue()
self.producerNums = producerNums
self.consumerNums = consumerNums
def start(self):
for i in range(self.producerNums):
self.producers.append(ProducerThread(self.queue))
for i in range(self.consumerNums):
self.consumers.append(ConsumerThread(self.queue))
for i in range(len(self.producers)):
self.producers[i].start()
for i in range(len(self.consumers)):
self.consumers[i].start()
for i in range(len(self.producers)):
self.producers[i].join()
for i in range(len(self.consumers)):
self.consumers[i].join() if __name__ == '__main__':
w = WorkShop(3, 4)
w.start()

最后写一个main模块:

from WorkShop import WorkShop

if __name__ == '__main__':
w = WorkShop(2, 3)
w.start()

使用Python实现生产者消费者问题的更多相关文章

  1. python进程——生产者消费者

    生产者消费者模型介绍 为什么要使用生产者消费者模型 生产者指的是生产数据的任务,消费者指的是处理数据的任务,在并发编程中,如果生产者处理速度很快,而消费者处理速度很慢,那么生产者就必须等待消费者处理完 ...

  2. python之生产者消费者模型

    #Auther Bob #--*--conding:utf-8 --*-- #生产者消费者模型,这里的例子是这样的,有一个厨师在做包子,有一个顾客在吃包子,有一个服务员在储存包子,这个服务员我们就可以 ...

  3. Python多线程-生产者消费者模型

    用多线程和队列来实现生产者消费者模型 # -*- coding:utf-8 -*- __author__ = "MuT6 Sch01aR" import threading imp ...

  4. (python)生产者消费者模型

    生产者消费者模型当中有两大类重要的角色,一个是生产者(负责造数据的任务),另一个是消费者(接收造出来的数据进行进一步的操作). 为什么要使用生产者消费者模型? 在并发编程中,如果生产者处理速度很快,而 ...

  5. python 之生产者消费者模型

    进程实现: import time,random from multiprocessing import Process,Queue def producer(name,q): count= 0 wh ...

  6. python并发——生产者消费者信号量实现

    介绍 写扫描器的时候,需要让资产扫描结果一出来(生产者),另外一边就会开个线程去运行漏洞扫描(消费者). 但是又不能让结果没出来,另外一边消费者就开始干活了. 代码 # *coding:UTF-8 * ...

  7. python实现生产者消费者模型

    生产者消费之模型就是,比如一个包子铺,中的顾客吃包子,和厨师做包子,不可能是将包子一块做出来,在给顾客吃,但是单线程只能这麽做,所以用多线程来执行,厨师一边做包子,顾客一边吃包子,当顾客少时,厨师做的 ...

  8. python 多线程 生产者消费者

    import threading import time import logging import random import Queue logging.basicConfig(level=log ...

  9. Python学习笔记——进阶篇【第九周】———线程、进程、协程篇(队列Queue和生产者消费者模型)

    Python之路,进程.线程.协程篇 本节内容 进程.与线程区别 cpu运行原理 python GIL全局解释器锁 线程 语法 join 线程锁之Lock\Rlock\信号量 将线程变为守护进程 Ev ...

随机推荐

  1. 关于background

    background目前有size;  color;  image;  repeat;position;attachtment; 作用分别是一:调整背景大小. 语法:background-size:a ...

  2. DRF最高封装的子类视图

    # 转载请留言联系 子类视图: 视图 作用 方法 父类 ListAPIView 查询多条数据 get GenericAPIView ListModelMixin CreateAPIView 新增一条数 ...

  3. Net Core 控制台程序使用Nlog 输出到log文件

    using CoreImportDataApp.Common; using Microsoft.Extensions.Configuration; using Microsoft.Extensions ...

  4. hdu 1316(大整数)

    How Many Fibs? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

  5. centos 查看 arp

    yum install tcpdump -y tcpdump arp :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: ...

  6. go语言多态接口样例

    感觉比java玄幻啊~~~ package main import ( "fmt" ) type notifier interface{ notify() } type user ...

  7. 安装Team Services Agent Win7

    现状:项目现时使用的是Team Services,使用Team Services可以控制其中的一台Build Server,从Github提取代码,并在Build Server进入编译打包处理(son ...

  8. 简单DP【p1934】封印

    Description 很久以前,魔界大旱,水井全部干涸,温度也越来越高.为了拯救居民,夜叉族国王龙溟希望能打破神魔之井,进入人界"窃取"水灵珠,以修复大地水脉.可是六界之间皆有封 ...

  9. 【bzoj1604】【[Usaco2008 Open]Cow Neighborhoods】简单的谈谈曼哈顿距离

    (最近p站上不去要死了) Description 了解奶牛们的人都知道,奶牛喜欢成群结队.观察约翰的N(1≤N≤100000)只奶牛,你会发现她们已经结成了几个"群".每只奶牛在吃 ...

  10. 1.1(Spring MVC学习笔记)初识SpringMVC及SpringMVC流程

    一.Spring MVC Spring MVC是Spring提供的一个实现了web MVC设计模式的轻量级Web框架. Spring优点:网上有,此处不复述. 二.第一个Spring MVC 2.1首 ...