queue is especially useful in threaded programming when information must be exchanged safely between multiple threads
queue在使用多进程之间交换安全信息的时候特别有用
class queue.Queue(maxsize=0) #先入先出
class queue.LifoQueue(maxsize=0) #last in fisrt out 
class queue.PriorityQueue(maxsize=0) #存储数据时可设置优先级的队列 优先级最小最先取出
exception queue.Empty 正常情况下当队列为空则阻塞,如果设置了get_nowaait则会报该异常
Exception raised when non-blocking get() (or get_nowait()) is called on a Queue object which is empty.
exception queue.Full 当设置了put_nowait,队列满时报异常

Exception raised when non-blocking put() (or put_nowait()) is called on a Queue object which is full.

Queue.qsize() 查看队列大小
Queue.empty() #return True if empty   队列为空返回True
Queue.full() # return True if full 
Queue.put(itemblock=Truetimeout=None) 上传到队列 正常当Q满了 在put就阻塞 如果timeout为True 则等待多少秒后直接抛异常
Queue.put_nowait(item)# 队列满直接抛异常
Queue.get(block=Truetimeout=None) #上传到队列,队列空了阻塞
Queue.get_nowait() 队列没数据直接抛异常
Queue.task_done() # q.task_done() 在完成一项工作之后,q.task_done() 函数向任务已经完成的队列发送一个信号. 
Queue.join() 实际上意味着等到队列为空,再执行别的操作
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import queue
class Foo(object):
def __init__(self,n):
self.n = n #q = queue.Queue(maxsize=30)
#q = queue.LifoQueue(maxsize=30)
q = queue.PriorityQueue(maxsize=30)
q.put((2,[1,2,3]))
#q.put(Foo(1))
q.put((10,1))
q.put((3,1))
q.put((5,30))
q.task_done()
q.join()
print(q.get())
print(q.get())
print(q.get())
print(q.get())

生产者消费者模型

#!/usr/bin/env python
# -*- coding:utf-8 -*-
import threading,queue
import time def consumer(n):
while True:
print("\033[32;1mconsumer [%s]\033[0m get task: %s" % (n,q.get()))
time.sleep(1)
q.task_done()#每get一次包子,像其他进程告知队列信息
def producer(n):
count = 1
while True:
#time.sleep(1)
#if q.qsize() <3:
print("prodcer [%s] produced a new task : %s" %(n,count))
q.put(count)
count +=1
q.join() #queue is emtpy # 等到队列为空在继续往下执行
print("all taks has been cosumed by consumers...") q = queue.Queue()
c1 = threading.Thread(target=consumer,args=[1,])
c2 = threading.Thread(target=consumer,args=[2,])
c3 = threading.Thread(target=consumer,args=[3,])
p = threading.Thread(target=producer,args=["XiaoYu",])
p2 = threading.Thread(target=producer,args=["LiuYao",])
c1.start()
c2.start()
c3.start()
p.start()
p2.start()

Python Queue队列的更多相关文章

  1. Python -- queue队列模块

    一 简单使用 --内置模块哦 import Queuemyqueue = Queue.Queue(maxsize = 10) Queue.Queue类即是一个队列的同步实现.队列长度可为无限或者有限. ...

  2. Python之路-python(Queue队列、进程、Gevent协程、Select\Poll\Epoll异步IO与事件驱动)

    一.进程: 1.语法 2.进程间通讯 3.进程池 二.Gevent协程 三.Select\Poll\Epoll异步IO与事件驱动 一.进程: 1.语法 简单的启动线程语法 def run(name): ...

  3. Python Queue(队列)

    Queue模块实现了多生产者.多消费者队列.当必须在多个线程之间安全地交换信息时,它在线程编程中特别有用,实现了所有必需的锁定语义. 一.该模块实现了三种类型的队列,它们的区别仅在于检索条目的顺序: ...

  4. 简短而有效的python queue队列解释

    Queue.qsize() 返回队列的大小  Queue.empty() 如果队列为空,返回True,反之False  Queue.full() 如果队列满了,返回True,反之False Queue ...

  5. Python 用队列实现多线程并发

    # Python queue队列,实现并发,在网站多线程推荐最后也一个例子,比这货简单,但是不够规范 # encoding: utf-8 __author__ = 'yeayee.com' # 由本站 ...

  6. Python自动化运维之16、线程、进程、协程、queue队列

    一.线程 1.什么是线程 线程是操作系统能够进行运算调度的最小单位.它被包含在进程之中,是进程中的实际运作单位. 一条线程指的是进程中一个单一顺序的控制流,一个进程中可以并发多个线程,每条线程并行执行 ...

  7. Python第十五天 datetime模块 time模块 thread模块 threading模块 Queue队列模块 multiprocessing模块 paramiko模块 fabric模块

    Python第十五天  datetime模块 time模块   thread模块  threading模块  Queue队列模块  multiprocessing模块  paramiko模块  fab ...

  8. Python之队列Queue

    今天我们来了解一下python的队列(Queue) queue is especiall useful in threaded programming when information must be ...

  9. python threading模块使用 以及python多线程操作的实践(使用Queue队列模块)

    今天花了近乎一天的时间研究python关于多线程的问题,查看了大量源码 自己也实践了一个生产消费者模型,所以把一天的收获总结一下. 由于GIL(Global Interpreter Lock)锁的关系 ...

随机推荐

  1. android 发短信

    SmsManager smsManager = SmsManager.getDefault(); List<String> divideContents = smsManager.divi ...

  2. Android开发--ScrollView的应用

    1.简介 当内容无法全部显示时,需要采取滚动的方式获取其与内容.其中,ScrollView为垂直滚动控件,HorizontalScrollView为水平滚动控件. 2.构建

  3. jquery mobile 教程

    简介:jQuery Mobile框架可以轻松的帮助我们实现非常好看的.可跨设备的Web应用程序.我们将后续的介绍中向大家介绍大量的代码及实例. jQuery一直以来都是非常流行的富客户端及Web应用程 ...

  4. 1172. Ship Routes

    http://acm.timus.ru/problem.aspx?space=1&num=1172 水题DP   大整数直接上java 代码: import java.math.BigInte ...

  5. PHP 函数(2)

    自定义函数: $name = "fakeface"; function dispalyName(){ echo "fakeface"; } function r ...

  6. Nullable<>

    Nullable<>是一个结构,但是怎么能让此结构=null,这是编译器的一个比较特殊的,他会new一个该结构的实例去赋值.Nullable<>有一个属性是HasValue,是 ...

  7. 在Web.config或App.config中的添加自定义配置

    .Net中的System.Configuration命名空间为我们在web.config或者app.config中自定义配置提供了完美的支持.最近看到一些项目中还在自定义xml文件做程序的配置,所以忍 ...

  8. Yii2-redis

    安装:composer require --prefer-dist yiisoft/yii2-redisredis 版本 >= 2.6.12 添加配置: 'components' => [ ...

  9. vbs常用代码

    在网上查找资料的时候发现好多经典的vbs代码收集起来也为了以后学习. VBS播放音乐 Dim wmp Set wmp = CreateObject("WMPlayer.OCX") ...

  10. 使用HTML5+CSS3制作圆角内发光按钮----示例

    <!doctype html> <html> <head> <meta charset="utf-8" /> <title&g ...