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)锁的关系 ...
随机推荐
- TestLink
TestLink的主要功能包括: 测试需求管理 测试用例管理 测试用例对测试需求的覆盖管理 测试计划的制定 测试用例的执行 大量测试数据的度量和统计功能 TestLink的主要特色包括: 支持多产品或 ...
- fis3运行项目的前准备
前几天搭建了fis3环境,但是不会运行项目.因为刚来公司前辈把项目打包给我,但是我之前没有做过这种项目. 今天前辈来了,教我几个命令行运行项目.但是没有成功..... 原因我的sass是单独安装的,没 ...
- C++中 接口的定义 COM
首先定义一个虚基类的接口,其中包含虚函数AddRef Release QueryInterface,(MFC 类IUnKnown unknwn.h)分别是增加减去引用计数和查询接口然后定义一个实现类, ...
- C#窗体 WinForm 文件操作
文件及文件夹操作 C/S:WinForm可以操作客户端文件 Client ServerB/S:浏览器服务 Brower Server 命名空间:using system .IO; 1. File类:文 ...
- Makefile.am链接openCV库的写法
6 INCLUDES = `pkg-config opencv --cflags` -I./ 17 bin_PROGRAMS+=SegRecogServerDeme 18 SegRecogServer ...
- 使用scp将文件/目录拷贝到另一台Linux主机上
如何将一台Linux主机上的文件或目录拷贝到另一台Linux主机上,scp命令可以实现该需求 前提条件:两台Linux主机处于同一网段,可以互相ping通 操作如下: 文件拷贝 ①将本地文件拷贝到远端 ...
- 我也来SplashScreen
SplashScreen,就是平时我们说的溅射屏幕,任何一个做过客户端程序的coder应该对它都不陌生,因为它能提升用户体验,让软件看上去更美.SplashScreenForm通常进入程序时是打开,主 ...
- Huawei校招机试中的猴子吃桃问题
//============================================================================ // Name : Monkey& ...
- POJ 2446 最小点覆盖
Chessboard Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 14787 Accepted: 4607 Descr ...
- MyEclipse中Maven的配置
之前在MyEclipse这个IDE中配置Maven,完成配置后启动Maven时出现-Dmaven.multiModuleProjectDirectory system propery is not s ...