python (协程)生产者,消费者
#coding=utf-
import gevent
from gevent.queue import Queue, Empty
import time
tasks = Queue(maxsize=) def worker(n):
try:
while True:
task = tasks.get(timeout=) # decrements queue size by
print('Worker %s got task %s' % (n, task))
gevent.sleep()
except Empty:
print('Worker %s says:\"Quitting time!\"' % (n)) def boss():
"""
Boss will wait to hand out work until a individual worker is
free since the maxsize of the task queue is .
""" for i in range(,):
tasks.put(i)
print('Assigned all work in iteration 1')#添加完毕 for i in range(,):
tasks.put(i)
print('Assigned all work in iteration 2')#添加完毕
t1=time.time()
gevent.joinall([
gevent.spawn(boss),
gevent.spawn(worker, 'steve'),
gevent.spawn(worker, 'john'),
gevent.spawn(worker, 'bob'),
])
print(time.time()-t1)
输出
Worker steve got task
Worker john got task
Worker bob got task
Worker steve got task
Worker john got task
Worker bob got task
Assigned all work in iteration
Worker steve got task
Worker john got task
Worker bob got task
Worker steve got task
Worker john got task
Worker bob got task
Worker steve got task
Worker john got task
Worker bob got task
Assigned all work in iteration
Worker steve got task
Worker john got task
Worker bob got task
Worker steve got task
Worker john says:"Quitting time!"
Worker bob says:"Quitting time!"
Worker steve says:"Quitting time!"
python (协程)生产者,消费者的更多相关文章
- python yield实现协程(生产者-消费者)
def customer(): r="" while True: n=yield r#,接收生产者的消息,并向消费者发送r print("customer receive ...
- day-5 python协程与I/O编程深入浅出
基于python编程语言环境,重新学习了一遍操作系统IO编程基本知识,同时也学习了什么是协程,通过实际编程,了解进程+协程的优势. 一.python协程编程实现 1. 什么是协程(以下内容来自维基百 ...
- 用yield实现python协程
刚刚介绍了pythonyield关键字,趁热打铁,现在来了解一下yield实现协程. 引用官方的说法: 与线程相比,协程更轻量.一个python线程大概占用8M内存,而一个协程只占用1KB不到内存.协 ...
- 00.用 yield 实现 Python 协程
来源:Python与数据分析 链接: https://mp.weixin.qq.com/s/GrU6C-x4K0WBNPYNJBCrMw 什么是协程 引用官方的说法: 协程是一种用户态的轻量级线程,协 ...
- Python核心技术与实战——十六|Python协程
我们在上一章将生成器的时候最后写了,在Python2中生成器还扮演了一个重要的角色——实现Python的协程.那什么是协程呢? 协程 协程是实现并发编程的一种方式.提到并发,肯很多人都会想到多线程/多 ...
- 操作系统OS,Python - 协程(Coroutine)
留坑 参考: https://en.wikipedia.org/wiki/Coroutine https://zh.wikipedia.org/wiki/%E5%8D%8F%E7%A8%8B http ...
- 5分钟完全掌握Python协程
本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,如有问题请及时联系我们以作处理 1. 协程相关的概念 1.1 进程和线程 进程(Process)是应用程序启动的实例,拥有代码.数据 ...
- Python协程与Go协程的区别二
写在前面 世界是复杂的,每一种思想都是为了解决某些现实问题而简化成的模型,想解决就得先面对,面对就需要选择角度,角度决定了模型的质量, 喜欢此UP主汤质看本质的哲学科普,其中简洁又不失细节的介绍了人类 ...
- Python 协程总结
Python 协程总结 理解 协程,又称为微线程,看上去像是子程序,但是它和子程序又不太一样,它在执行的过程中,可以在中断当前的子程序后去执行别的子程序,再返回来执行之前的子程序,但是它的相关信息还是 ...
- 终结python协程----从yield到actor模型的实现
把应用程序的代码分为多个代码块,正常情况代码自上而下顺序执行.如果代码块A运行过程中,能够切换执行代码块B,又能够从代码块B再切换回去继续执行代码块A,这就实现了协程 我们知道线程的调度(线程上下文切 ...
随机推荐
- 监控nginx服务
转自:http://www.cnblogs.com/silent2012/p/5310500.html 在Nginx的插件模块中有一个模块stub_status可以监控Nginx的一些状态信息,默认安 ...
- Django之MVC和MTV
一. MVC MVC 是一种使用 MVC(Model View Controller 模型-视图-控制器)设计创建 Web 应用程序的模式: Model(模型)表示应用程序核心(比如数据库记录列表). ...
- 哪些个在 Sublime Text 下,"任性的" 好插件!
我在sublime里面安装了以下有利于项目开发高效的插件: 1:SVN 源代码版本控制 2:LiveReload 浏览器实时刷新 3:jsMinifier 压缩 j ...
- PostgreSQL 10首个测试版本发布
mysql 从5.7到8.0,pg从9.6到10,干起来了.. PostgreSQL 10 的首个测试版发布了,此版本包含 PostgreSQL 10 最终将提供的所有功能的预览.当然,有些细节将在最 ...
- 关于innodb_flush_log_at_trx_commit、innodb_flush_method、innodb_log_block_size和fsync()、O_DIRECT、iops、云盘的关系与总结
想着整理关于innodb_flush_log_at_trx_commit.innodb_flush_method.innodb_log_block_size和fsync().O_DIRECT.iops ...
- sed/awk advance
$ echo test.file [Group1]cacheways = 19 [Group2]cacheways = 19 $ ls test.sh FILE=test.file2 # Set Gr ...
- docker 给运行的容器映射本地端口
1.提交运行中的容器为一个镜像 (这样不会丢失在容器的各种操作) docker commit tang tang1 ### tang(运行容器名称) tang1(生成镜像名称) 2 ...
- 我的QML
1.键盘加Text import QtQuick 2.7 import QtGraphicalEffects 1.0 Rectangle{ width:; height:; color:"# ...
- 王之泰201771010131《面向对象程序设计(java)》第一周学习总结
王之泰201771010131<面向对象程序设计(java)>第一周学习总结 第一部分:课程准备部分 填写课程学习 平台注册账号, 平台名称 注册账号 博客园:www.cnblogs.co ...
- grub基本应用
一.基本概念 GRUB(boot loader): GRand Unified Bootloader 两个版本: grub .x: grup legacy grub .x: grub2 grub ...