import time
import random
import queue
from multiprocessing import Process,Queue
案例一:
def consumer(q,name):
while True:
food = q.get()
if food is None:
break
time.sleep(random.uniform(0.5,1))
print('%s消费了%s'%(name,food)) def producer(q,name,food):
for i in range(5):
time.sleep(random.uniform(0.3,0.8))
print('%s生产了%s'%(name,food))
q.put(food+str(i)) if __name__ == '__main__':
q = Queue()
c1 = Process(target=consumer, args=(q, "消费者1"))
c2 = Process(target=consumer, args=(q, "consuer2"))
# c1.daemon = True
c1.start()
c2.start()
p1 = Process(target=producer, args=(q, "生产者1", "产品1"))
p2 = Process(target=producer, args=(q, "生产者2", "产品2"))
p1.start()
p2.start() p1.join()
p2.join()
q.put(None)
q.put(None)
案例二:
承接上面导入的包
from multiprocessing import Process,JoinableQueue

def consumer(q,client):
while True:
product = q.get()
time.sleep(random.uniform(0.6,2.2))
print('%s购买了 %s' %(client,product))
q.task_done() def producer(q,client,product):
for i in range(5):
time.sleep(random.uniform(0.2,0.9))
print('%s 制造了 %s' %(client,product+str(i)))
q.put(product+str(i)) if __name__ == '__main__':
jq = JoinableQueue()
p1 = Process(target=consumer,args=(jq,'消费者one'))
p1.daemon = True
p1.start() p2 = Process(target=producer,args=(jq,'生产者one','黄金'))
p2.start()
p2.join()
jq.join()
二的执行结果之一:
生产者one 制造了 黄金0
生产者one 制造了 黄金1
生产者one 制造了 黄金2
消费者one购买了 黄金0
生产者one 制造了 黄金3
生产者one 制造了 黄金4
消费者one购买了 黄金1
消费者one购买了 黄金2
消费者one购买了 黄金3
消费者one购买了 黄金4
 

Python生产者producer和consumer消费者案例写法,含有多线程,包含队列queue、JoinableQueue队列的用法的更多相关文章

  1. python编写producer、consumer

    自主producer.consumer 首先在不同的终端,分别开启两个consumer,保证groupid一致 ]# python consumer_kafka.py 执行一次producer ]# ...

  2. Python与数据结构[2] -> 队列/Queue[0] -> 数组队列的 Python 实现

    队列 / Queue 数组队列 数组队列是队列基于数组的一种实现,其实现类似于数组栈,是一种FIFO的线性数据结构. Queue: <--| 1 | 2 | 3 | 4 | 5 |<-- ...

  3. 生产者和消费者模型producer and consumer(单线程下实现高并发)

    #1.生产者和消费者模型producer and consumer modelimport timedef producer(): ret = [] for i in range(2): time.s ...

  4. python生产者消费者模型

    业界用的比较广泛,多线程之间进行同步数据的方法,解决线程之间堵塞,互相不影响. server --> 生产者 client --> 消费者 在一个程序中实现又有生产者又有消费者 ,生产者不 ...

  5. java学习之生产者和消费者案例

    package com.gh.thread; /** * 生产者和消费者案例 * wait和sleep的区别 * wait不让出监视器锁,sleep让出监视器的锁 * @author ganhang ...

  6. Python 生产者与消费者模型

    定义: 在并发编程中使用生产者和消费者模式能够解决绝大多数并发问题.该模式通过平衡生产线程和消费线程的工作能力来提高程序的整体处理数据的速度.     为什么要使用生产者和消费者模式 在线程世界里,生 ...

  7. GUC-13 生产者和消费者案例-旧

    /* * 生产者和消费者案例 */ public class TestProductorAndConsumer { public static void main(String[] args) { C ...

  8. GUC-13 生产者消费者案例

    import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.Lock; import java.uti ...

  9. [b0029] python 归纳 (十四)_队列Queue实现生产者消费者

    # -*- coding: UTF-8 -*- """ 多线程的生产者,消费者 使用队列Queue """ import Queue imp ...

随机推荐

  1. android sdk 安装 配置

    下载android sdk manager:http://dl.google.com/android/installer_r24.4.1-windows.exe 打开sdk manager 在tool ...

  2. git宝典—应付日常工作使用足够的指北手册

    最近公司gitlab又迁移,一堆git的命令骚操作,然鹅git命令,感觉还是得复习下——其实,git现在界面操作工具蛮多,比如intellij 自带的git操作插件就不错,gitlab github ...

  3. 303. Range Sum Query - Immutable(动态规划)

    Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...

  4. Kindle:自动追更之发送邮件

    @echo off setlocal enabledelayedexpansion set from=Kindlekindle设置好信任的邮箱set pw=密码 set to=Kindle邮箱 cd ...

  5. selenium 定位密码软键盘

    from selenium import webdriver import time driver = webdriver.Chrome() driver.maximize_window() driv ...

  6. qemu中的内存管理

    qemu负责模拟虚机的外设,因此虚机的线性地址空间主要由qemu进行管理,也就是确定线性地址空间中哪段地址属于哪个设备或者DRAM或者其他的什么. 1.数据结构 1.RAMBLOCK (最直接接触ho ...

  7. CodeForces - 1033A

    Alice and Bob are playing chess on a huge chessboard with dimensions n×nn×n. Alice has a single piec ...

  8. Centos7配置SVN服务端

    环境 Centos 7 SVN 1.7 安装SVN Shell> yum install subversion -y 准备配置和仓库 Shell> mkdir -p /mydata/rep ...

  9. Integer类toString(int i,int radix)方法

    Integer类toString(int i,int radix)方法: 首先抛出java的api中的介绍: public static String toString(int i, int radi ...

  10. SpringBoot Tomcat启动报错

    中间的桥梁就是下面这个依赖 <dependency> <groupId>org.slf4j</groupId> <artifactId>jcl-over ...