#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 (协程)生产者,消费者的更多相关文章

  1. python yield实现协程(生产者-消费者)

    def customer(): r="" while True: n=yield r#,接收生产者的消息,并向消费者发送r print("customer receive ...

  2. day-5 python协程与I/O编程深入浅出

    基于python编程语言环境,重新学习了一遍操作系统IO编程基本知识,同时也学习了什么是协程,通过实际编程,了解进程+协程的优势. 一.python协程编程实现 1.  什么是协程(以下内容来自维基百 ...

  3. 用yield实现python协程

    刚刚介绍了pythonyield关键字,趁热打铁,现在来了解一下yield实现协程. 引用官方的说法: 与线程相比,协程更轻量.一个python线程大概占用8M内存,而一个协程只占用1KB不到内存.协 ...

  4. 00.用 yield 实现 Python 协程

    来源:Python与数据分析 链接: https://mp.weixin.qq.com/s/GrU6C-x4K0WBNPYNJBCrMw 什么是协程 引用官方的说法: 协程是一种用户态的轻量级线程,协 ...

  5. Python核心技术与实战——十六|Python协程

    我们在上一章将生成器的时候最后写了,在Python2中生成器还扮演了一个重要的角色——实现Python的协程.那什么是协程呢? 协程 协程是实现并发编程的一种方式.提到并发,肯很多人都会想到多线程/多 ...

  6. 操作系统OS,Python - 协程(Coroutine)

    留坑 参考: https://en.wikipedia.org/wiki/Coroutine https://zh.wikipedia.org/wiki/%E5%8D%8F%E7%A8%8B http ...

  7. 5分钟完全掌握Python协程

    本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,如有问题请及时联系我们以作处理 1. 协程相关的概念 1.1 进程和线程 进程(Process)是应用程序启动的实例,拥有代码.数据 ...

  8. Python协程与Go协程的区别二

    写在前面 世界是复杂的,每一种思想都是为了解决某些现实问题而简化成的模型,想解决就得先面对,面对就需要选择角度,角度决定了模型的质量, 喜欢此UP主汤质看本质的哲学科普,其中简洁又不失细节的介绍了人类 ...

  9. Python 协程总结

    Python 协程总结 理解 协程,又称为微线程,看上去像是子程序,但是它和子程序又不太一样,它在执行的过程中,可以在中断当前的子程序后去执行别的子程序,再返回来执行之前的子程序,但是它的相关信息还是 ...

  10. 终结python协程----从yield到actor模型的实现

    把应用程序的代码分为多个代码块,正常情况代码自上而下顺序执行.如果代码块A运行过程中,能够切换执行代码块B,又能够从代码块B再切换回去继续执行代码块A,这就实现了协程 我们知道线程的调度(线程上下文切 ...

随机推荐

  1. asp.net热门框架

    http://developer.51cto.com/art/201501/464292.htm

  2. MyEclipse新建Server项目

  3. 安装虚拟环境virtualenvwrapper和django

    以下操作在windows平台进行 1.安装虚拟环境virtualenvwrapper 首先需要安装python管理工具pip,安装完python3.7之后自带了pip,可通过where pip查看管理 ...

  4. Pycharm创建Django项目并访问Django

    Pycharm创建Django项目并访问Django 一.Django插件 如果没有Django插件,需要先安装Django插件. 1,首先点击Default Settings,打开设置页面: 2,打 ...

  5. 针对数据泵导出 (expdp) 和导入 (impdp)工具性能降低问题的检查表 (文档 ID 1549185.1)

    针对数据泵导出 (expdp) 和导入 (impdp)工具性能降低问题的检查表 (文档 ID 1549185.1) 文档内容 适用于: Oracle Database – Enterprise Edi ...

  6. eclispe设置workspace text file encoding

    在windows下开发,经常会遇到eclipse新导入的工程 java代码中的注释或者字符串中文显示乱码,每次都要一个个项目更改麻烦,特地找了下,可通过如下方法一次性设置.

  7. ql常见面试题 受用了

    //转至:http://www.cnblogs.com/GT_Andy/archive/2009/12/25/1921911.html 1. 用一条SQL 语句 查询出每门课都大于80 分的学生姓名 ...

  8. oracle RAC如何正确地删除ASM磁盘组

    1.登录到命令行 切换到grid用户 [grid@swnode1 ~]$ sqlplus / as sysasm SQL*Plus: Release Production on Wed May :: ...

  9. 更改 centos 7的源为 阿里源

    阿里源的网址在这里:http://mirrors.aliyun.com/repo/ 一.进入源文件存放目录 cd /etc/yum.repos.d 二.安装基本源: 1.如果要备份原来的源文件 sud ...

  10. Kettle 连接 Oracle 问题总结

    一. Driver class 'oracle.jdbc.driver.OracleDriver' could not be found, make sure the 'Oracle' driver ...