Python Queue队列
- 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()(orget_nowait()) is called on aQueueobject 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(item, block=True, timeout=None) 上传到队列 正常当Q满了 在put就阻塞 如果timeout为True 则等待多少秒后直接抛异常
Queue.put_nowait(item)# 队列满直接抛异常Queue.get(block=True, timeout=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队列的更多相关文章
- Python -- queue队列模块
一 简单使用 --内置模块哦 import Queuemyqueue = Queue.Queue(maxsize = 10) Queue.Queue类即是一个队列的同步实现.队列长度可为无限或者有限. ...
- Python之路-python(Queue队列、进程、Gevent协程、Select\Poll\Epoll异步IO与事件驱动)
一.进程: 1.语法 2.进程间通讯 3.进程池 二.Gevent协程 三.Select\Poll\Epoll异步IO与事件驱动 一.进程: 1.语法 简单的启动线程语法 def run(name): ...
- Python Queue(队列)
Queue模块实现了多生产者.多消费者队列.当必须在多个线程之间安全地交换信息时,它在线程编程中特别有用,实现了所有必需的锁定语义. 一.该模块实现了三种类型的队列,它们的区别仅在于检索条目的顺序: ...
- 简短而有效的python queue队列解释
Queue.qsize() 返回队列的大小 Queue.empty() 如果队列为空,返回True,反之False Queue.full() 如果队列满了,返回True,反之False Queue ...
- Python 用队列实现多线程并发
# Python queue队列,实现并发,在网站多线程推荐最后也一个例子,比这货简单,但是不够规范 # encoding: utf-8 __author__ = 'yeayee.com' # 由本站 ...
- Python自动化运维之16、线程、进程、协程、queue队列
一.线程 1.什么是线程 线程是操作系统能够进行运算调度的最小单位.它被包含在进程之中,是进程中的实际运作单位. 一条线程指的是进程中一个单一顺序的控制流,一个进程中可以并发多个线程,每条线程并行执行 ...
- Python第十五天 datetime模块 time模块 thread模块 threading模块 Queue队列模块 multiprocessing模块 paramiko模块 fabric模块
Python第十五天 datetime模块 time模块 thread模块 threading模块 Queue队列模块 multiprocessing模块 paramiko模块 fab ...
- Python之队列Queue
今天我们来了解一下python的队列(Queue) queue is especiall useful in threaded programming when information must be ...
- python threading模块使用 以及python多线程操作的实践(使用Queue队列模块)
今天花了近乎一天的时间研究python关于多线程的问题,查看了大量源码 自己也实践了一个生产消费者模型,所以把一天的收获总结一下. 由于GIL(Global Interpreter Lock)锁的关系 ...
随机推荐
- informix dbaccess 常用执行方式及常见技巧
假设: A.数据库servername: testserver B.数据库名:testdb C.SQL脚本文件: sqlfile.sql create table test_table(c1 inte ...
- Java—类的封装、继承与多态
一.类和对象 1.类 类是数据以及对数据的一组操作的封装体. 类声明的格式: 类声明 { 成员变量的声明: 成员方法的声明及实现: } 1.1 声明类 [修饰符] class 类<泛型> ...
- python语言的优点和缺点
python作为一门高级编程语言,它的诞生虽然很偶然,但是它得到程序员的喜爱却是必然之路. 龟叔给Python的定位是"优雅"."明确"."简单&qu ...
- C#微信公众号接口开发实例-高级接口-申请带参数的二维码
最近公司涉及到微信绑定用户,做了高级接口-申请带参数的二维码,总结了下微信开发接口.微信接口开发都是除了消息用的xml 回复基本上都是用json的形式传递信息(post/get),开发的方法基本都是一 ...
- 解决cocopods不提示第三方库名字的方法
在使用第三方类库时,使用cocoaPods是非常方便的,具体使用方法可以参考:CocoaPods安装和使用教程 的安装使用方法.今天讨论的问题是,我在使用的时候遇到了一些问题:用cocoaPod si ...
- webix custom component-九宫格
上篇大致讲了对源码的理解,这篇展示一个初步的九宫格控件.直接上源码: webix.protoUI({ name:"grid", $init:function(config){ co ...
- Windows 通用应用尝试开发 “51单片机汇编”第二次更新总结
一.前言 昨天更新了10天前上架到windows8.1平台和windowsphone平台的通用应用“51单片机汇编”,总要是添加了动态磁贴以及ListView的Groupstyle应用.下面主要主要复 ...
- sql 创建数据库
CREATE DATABASE [NET_CN] ON PRIMARY( NAME=N'NET_CN',FILENAME= N'D:\Data\NET_CN.mdf',SIZE = 5120KB,MA ...
- JavaScript本地对象 内置对象 宿主对象
在ECMAScript中,所有对象并非同等创建的. 一般来说,可以创建并使用的对象有3种:本地对象.内置对象和宿主对象. 1. 本地对象 ECMA-262把本地对象(native obje ...
- candence 知识积累3
1. PCB板型: 1.新建PCB:PCB design ,新建的类型为board ,输入名称和保存位置,设置图纸参数.网格参数. 2.建立PCB板外框:菜单Add下选择相应的工具.在Option选项 ...