gevent模块学习(二)
import gevent
from gevent.queue import Queue # 队列是一个排序的数据集合 put和get都有非阻塞模式,put_nowait和get_nowait不会阻塞,在操作不能完成时抛出gevent.queue.Empty或gevent.queue.Full异常 tasks = Queue() def worker(n):
while not tasks.empty():
task = tasks.get()
print('worker %s got task %s'%(n, task))
gevent.sleep(0) print('Quitting time!') def boss():
for i in xrange(1, 25):
tasks.put_nowait(i) gevent.spawn(boss).join() gevent.joinall([
gevent.spawn(worker, 'steve'),
gevent.spawn(worker, 'john'),
gevent.spawn(worker, 'nancy'),
])
# coding:utf-8 import gevent
from gevent.queue import Queue, Empty, LifoQueue, PriorityQueue # 先进先出
# 先进后出
# 优先级队列 class Job():
def __init__(self, prority, des):
self.prority = prority
self.des = des def __cmp__(self, other):
return cmp(self.prority, other.prority) def __str__(self):
return 'prority %d des %s'%(self.prority, self.des) q = LifoQueue()
q.put(Job(3, 'mid job'))
q.put(Job(10, 'low job'))
q.put(Job(1, 'important job'))
while not q.empty():
job = q.get()
print(job)
joinableQueue JoinableQueue 比Queue多了task_done() 与join()两个函数,都是专用于全球进行编程的,当然多是生产者消费者问题。 task_done() 是用在get()后,告诉os, 我get完了,join()是说Queue里所有的items都被拿出来搞完了。 --------------------- 参考来自 heavendai 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/heavendai/article/details/24380325?utm_source=copy
# coding:utf-8
import gevent
from gevent import Greenlet from gevent.queue import JoinableQueue, Empty
# 主队列 class Actor(Greenlet):
def __init__(self, data):
self.data = data
Greenlet.__init__(self) def _run(self):
result = do_something(self.data)
return result def do_something(data):
print('the data is %s'%data) queue = JoinableQueue() def boss():
for i in xrange(1, 1000):
queue.put_nowait(i)
boss() def work():
while True:
try:
data = queue.get(timeout=5)
print('data is %s'%data)
except Empty:
gevent.sleep(0.5)
break
finally:
queue.task_done()
# queue.join()
# 交付给Actor
# if data:
# g1 = Actor(data)
# g1.start()
# g1.join()
for i in range(5):
gevent.spawn(work)
queue.join()
gevent模块学习(二)的更多相关文章
- Python urllib和urllib2模块学习(二)
一.urllib其它函数 前面介绍了 urllib 模块,以及它常用的 urlopen() 和 urlretrieve()函数的使用介绍.当然 urllib 还有一些其它很有用的辅助方法,比如对 ur ...
- gevent模块学习(一)
1.Event类,事件主要用于Greenlet之间的异步通信 e = gevent.event.Event() -> Event 说明: 创建一个信号对象 e.set() -> None ...
- Python3 学习第九弹: 模块学习二之文件管理模块
os模块 提供访问操作系统的接口 1> name 获得当前操作系统 其中 'nt' 是 windows 'posix' 是 linux 2> environ 获得当前系统的环境变量的字典, ...
- gevent模块学习(四)
gevent.spawn会对传入的子任务集合进行调度,gevent.joinall 方法会阻塞当前程序,除非所有的greenlet都执行完毕,才会退出程序 公有方法 gevent.spawn(cls, ...
- gevent模块学习(三)
3. Group类,常用于不限制数量的管理异步任务的分组且可搜集运行结果 g = gevent.pool.Group(*args) -> Group 说明: 创建一个组对象,其实就是一个不限gr ...
- python模块学习(二)
configparser模块 软件常见文档格式如下: [DEFAULT]ServerAliveInterval = 45Compression = yesCompressionLevel = 9For ...
- nodejs的mysql模块学习(二)连接数据库
nodejs连接mysql的方式有两种 官方建议的第一种是 let mysql = require('mysql'); let connection = mysql.createConnection( ...
- Python学习 :常用模块(二)
常用模块(二) 四.os模块 os模块是与操作系统交互的一个接口,用于对操作系统进行调用 os.getcwd() # 提供当前工作目录 os.chdir() # 改变当前工作目录 os.curdir( ...
- Node学习(二) --使用http和fs模块实现一个简单的服务器
1.创建一个www目录,存储静态文件1.html.1.jpg. * html文件内容如下: 12345678910111213 <html lang="en">< ...
随机推荐
- SQL Server 复制表结构以及数据,去除表中重复字段
--复制另一个数据库中的某张表的结构及数据--select * from Test.dbo.TestTable(查询表中所有数据) --into [表名] 插入当前数据库新表,如果没有该表就创建 se ...
- 系统管理--配置Gitlab
很多教程都有配这个,但这个又不能用于”源码管理”模块拉取代码,我一直很困惑这个配置有什么用,然后就找到了该插件的github项目地址才弄明白,链接:https://github.com/jenkins ...
- centos6/7破解root密码的操作
Centos 6 1.开机按“Esc”键 2.按e键进入编辑模式后,选择Kernel /vmlinuz-2.6.92......项 3.进入该编辑模式后,在quiet后面输入simple或者数字1后, ...
- VB代码收集
1.随机获取5位验证码? 需求: 创建一个Label1:名称为随机验证码生成 创建一个Label2:名称为为空,属性BorderStyle=1 创建一个CommandButton:名称为获取随机码 代 ...
- Python中 sys.argv[]的用法简明解释
sys.argv[]就是一个从程序外部获取参数的桥梁,这个“外部”很关键.因为我们从外部取得的参数可以是多个,所以获得的是一个列表(list),也就是说sys.argv其实可以看作是一个列表,所以才能 ...
- 彻底清除 Windows 服务
如果服务已经停止, 或从注册表中删除, 但是在任务管理器中仍能看到服务躺在列表里面. 只需要找到服务的PID, 然后运行命令: taskkill /PID 服务的PID /f 即可.
- 关于文件目录等的特殊权限setuid, setgid , sticky chattr, lsattr
有三种特殊权限 总之, 设置这些特殊权限有两种方法, 一是使用 chmod ugo的方式, 另一个是 使用 数字的方式, 通常的读写执行 权限 是 3位 数字, 那么 特殊权限 就用 4位数字, 而且 ...
- HttpClient throws TaskCanceledException on timeout
error msg: HttpClient throws TaskCanceledException on timeout HttpClient is throwing a TaskCanceledE ...
- Nilearn 小记
4.绘制脑图像 4.1 绘图功能 当打开了太多图像而不关闭时,会出现如下问题: 每次调用绘图函数都会创建一个新图像.当在非交互式设置(例如脚本或程序)中使用时,这些图像不会显示,但会常驻于内存中并最终 ...
- 使用win10的开始屏幕,在系统中设置简洁、快捷桌面
前几天入手了一个本本,由于之前电脑使用的柠檬桌面软件和现在本本的分辨率不适应,意外发现win10自带的开始屏幕整理桌面也是很有意思,再加上触摸板的手势,瞬间觉得整个电脑都清洁许多.废话少说,开始上料. ...