博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python--小功能应用
阅读量:4677 次
发布时间:2019-06-09

本文共 861 字,大约阅读时间需要 2 分钟。

#生产者消费者模型(单线程高并发)
1、低效率模式  产完在统一给
import timedef producter():    ret = []    for i in range(10):        time.sleep(0.1)        ret.append(i)    return  retdef consumer(res):    for index,baozi in enumerate(res):        time.sleep(0.1)        print('第%s个人,吃了%s' %(index,baozi))res = producter()print(consumer(res))
2、高效率模式  产一个给一个
import timedef consumer(name):    print('我是[%s],我在排队买包子' %name)    while True:        baozi = yield        time.sleep(1)   #模拟在achun在吃包子        print('%s很开心,%s很好吃' %(name,baozi))def producter():    population1 = consumer('achun')   #生成器函数    population1.__next__()    population2 = consumer('alin')  # 生成器函数    population2.__next__()    for i in range(10):        time.sleep(1)   #模拟在在做包子        population1.send('鲜肉菜包 %s' %i)        population2.send('鲜肉菜包 %s' %i)producter()

 

 

转载于:https://www.cnblogs.com/Essaycode/p/10126515.html

你可能感兴趣的文章
import configparser
查看>>
勇士闯迷宫
查看>>
mysql-冗余和重复索引
查看>>
backbone学习笔记0
查看>>
移动端调试 weinre
查看>>
JDK自带工具keytool生成ssl证书
查看>>
总结下抽象类Abstract和虚方法Virtual(易混点)
查看>>
CSUOJ 1248 非变性聚丙烯酰胺凝胶电泳
查看>>
POJ 2823 Sliding Window
查看>>
element-UI el-table二次封装
查看>>
Quartz.net Cron表达式
查看>>
类名.class
查看>>
vue组件
查看>>
Tensorflow without a phd master 1——Jetson nano初体验5
查看>>
[Lua]内存泄漏与垃圾回收
查看>>
linux 多进程绑定问题
查看>>
HTML5语义化标签
查看>>
【Java】基础篇-LinkedList
查看>>
006 输入和输出
查看>>
Python3.5.2中的变量介绍
查看>>