2. Queue类,常用用于Greenlet之间的异步共享
q = gevent.queue.Queue(maxsize=None, items=None) -> Queue
说明: 创建一个指定大小包含指定items的队列对象
q.empty() -> Boolean
说明: 队列是否为空
q.full() -> Boolean
说明: 队列是否已满,如果初始化时maxsize为None时队列永远不会满,除非内存耗尽
q.get(block=True, timeout=None) -> obj
说明: 队列尾部弹出并返回末尾元素,block为True如果队列为空会一直等待,否则会抛出gevent.queue.Empty异常
q.get_nowait() -> obj
说明: 同q.get(block=False),如果队列为空直接返回gevent.queue.Empty
q.put(item, block=True, timeout=None) -> None
说明: 队列头部插入item,如果block为True则等待队列有空间时再插入,如果超出timeout则抛出Timeout异常,否则直接抛出gevent.queue.Full异常
q.put_nowait(self, item) -> None
说明: 同q.put(block=False),如果队列满直接返回gevent.queue.Full
q.qsize() -> int
说明: 返回队列的元素数
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模块学习(二)的更多相关文章

  1. Python urllib和urllib2模块学习(二)

    一.urllib其它函数 前面介绍了 urllib 模块,以及它常用的 urlopen() 和 urlretrieve()函数的使用介绍.当然 urllib 还有一些其它很有用的辅助方法,比如对 ur ...

  2. gevent模块学习(一)

    1.Event类,事件主要用于Greenlet之间的异步通信 e = gevent.event.Event() -> Event 说明: 创建一个信号对象 e.set() -> None ...

  3. Python3 学习第九弹: 模块学习二之文件管理模块

    os模块 提供访问操作系统的接口 1> name 获得当前操作系统 其中 'nt' 是 windows 'posix' 是 linux 2> environ 获得当前系统的环境变量的字典, ...

  4. gevent模块学习(四)

    gevent.spawn会对传入的子任务集合进行调度,gevent.joinall 方法会阻塞当前程序,除非所有的greenlet都执行完毕,才会退出程序 公有方法 gevent.spawn(cls, ...

  5. gevent模块学习(三)

    3. Group类,常用于不限制数量的管理异步任务的分组且可搜集运行结果 g = gevent.pool.Group(*args) -> Group 说明: 创建一个组对象,其实就是一个不限gr ...

  6. python模块学习(二)

    configparser模块 软件常见文档格式如下: [DEFAULT]ServerAliveInterval = 45Compression = yesCompressionLevel = 9For ...

  7. nodejs的mysql模块学习(二)连接数据库

    nodejs连接mysql的方式有两种 官方建议的第一种是 let mysql = require('mysql'); let connection = mysql.createConnection( ...

  8. Python学习 :常用模块(二)

    常用模块(二) 四.os模块 os模块是与操作系统交互的一个接口,用于对操作系统进行调用 os.getcwd() # 提供当前工作目录 os.chdir() # 改变当前工作目录 os.curdir( ...

  9. Node学习(二) --使用http和fs模块实现一个简单的服务器

    1.创建一个www目录,存储静态文件1.html.1.jpg. * html文件内容如下: 12345678910111213 <html lang="en">< ...

随机推荐

  1. LP-KPN

    LP-KPN 网络结构 网络解析 1. 网络结构中绿色星星标志 公式.其实就是用预测出来的核在原图片经过Laplacian pyramid decomposes 后的图片上进行卷积运算.所以应该使用p ...

  2. java面试经常问到的计算机网络问题

    GET 和 POST 的区别 GET请注意,查询字符串(名称/值对)是在 GET 请求的 URL 中发送的:/test/demo_form.asp?name1=value1&name2=val ...

  3. Genymotion-Android模拟器提示"Unable to connect to the Genymotion server. Please check your Internet connection."解决方法

    昨天刚装的Genymotion,昨晚还用得好好的. 今晚开机,重新打开Genymotion,却提示:"Unable to connect to the Genymotion server. ...

  4. MySQL-ISNULL()、IFNULL()和NULLIF()函数

    以下三个函数都可以用于where子条件,作为数据删除.更新的记录定位依据. 如: SELECT * FROM usergrade WHERE ISNULL(USERNAME); 一.ISNULL(ex ...

  5. delphi 利用 InterlockedCompareExchange 实现主线程维一锁等待

    在进行资源锁定时,一般是线程之间进行交互,很少需要在主线程也对资源进行锁定. 不过在一些复杂的业务中,存在子线程与主线程的交互,且一些资源也同步在主线程中使用时,主线程资源锁,就有存在的必要. 假定有 ...

  6. js 计算两个时间戳之间相隔天数

    var start=1491789600000;//2017-4-10 10:00 var end=1494381600000;//2017-5-10 10:00 var utc=end-start; ...

  7. Pandas:深市股票代码前补足0

    #深市代码前补充0----------------- df[' #先增加一列 #将2列合并为新列 df['代码合并'] = df['补充'] + df['股票代码'] #再取后6位 df['股票代码' ...

  8. kubernets code-generator

    REF: how-to-generate-client-codes-for-kubernetes-custom-resource-definitions-crd Firstly we need to ...

  9. Java链接MySQL数据库的用配置文件和不用配置文件的代码

    1.利用配置文件(db.properties)链接MySQL数据库 package tool; import java.io.FileInputStream;import java.sql.Conne ...

  10. 7.20 Codeforces Beta Round #8

    链接:codeforces.com/contest/8 A 原因:RE,fantasy 的字符串的长度可能大于原字符串. B 题意:上下左右走,可能要避让障碍,问是否存在一个地图使得给定的路径为当前最 ...