#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. MFC超链接

    最近写一个小的对话框程序时,想加一个文本超链接,研究了一下,发上自己的研究成果,供大家参考.下面说说完整的步骤. (假定静态文本ID为ID_STATIC) 首先,设置鼠标的形状及响应鼠标点击 第一步, ...

  2. eHR自动同步获取LDAP中的邮箱地址

    背景:公司里有eHR系统,有网域,IBM Lotus Notes邮件系统,新人入职会在eHR系统里提前建好档案,网域帐号.邮箱帐号均会在入职前提前建好,因为邮箱帐号是晚于eHR建档的,因此在eHR建档 ...

  3. 注册页面的JSON响应方式详细分析(与前端页面交互方式之一)

    控制器层 需求分析: 访问路径:`/user/reg.do` //自己根据功能需求设定的请求参数:`username=xx&password=xx&&phone=xx& ...

  4. ansible中的playbook详解

    首先简单说明一下playbook,playbook是什么呢?根本上说playbook和shell脚本没有任何的区别,playbook就像shell一样,也是把一堆的命令组合起来,然后加入对应条件判断等 ...

  5. APP端上传图片 - php接口

    $base64="iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAYAAADgdz34AAAABHNCSVQICAgIfAhkiAAAAbRJREFUSIntlDFPFF ...

  6. Capture HTML Canvas as gif/jpg/png/pdf?

    https://stackoverflow.com/questions/923885/capture-html-canvas-as-gif-jpg-png-pdf https://stackoverf ...

  7. torchvision.datasets.ImageFolder数据加载

    ImageFolder 一个通用的数据加载器,数据集中的数据以以下方式组织 root/dog/xxx.png root/dog/xxy.png root/dog/xxz.png root/cat/12 ...

  8. sql 思路

    先 django 定好sql框架 再 sqlalchemy 根据框架写...

  9. 深入理解softmax函数

    Softmax回归模型,该模型是logistic回归模型在多分类问题上的推广,在多分类问题中,类标签  可以取两个以上的值.Softmax模型可以用来给不同的对象分配概率.即使在之后,我们训练更加精细 ...

  10. Linux UinxODBC安装与配置

    Linux UinxODBC安装与配置 一.简介 ODBC是Open Database Connect 即开发数据库互连的简称,它是一个用于访问数据库的统一界面标准.ODBC引入一个公共接口以解决不同 ...