Pipe()只能有两个端点。

Queue()可以有多个生产者和消费者。

何时使用它们

如果您需要两个以上的点进行通信,请使用Queue() 。

如果你需要绝对性能, Pipe()要快得多,因为Queue()是建立在Pipe()之上的。

绩效基准

假设您想要生成两个进程并尽快在它们之间发送消息。 这些是使用Pipe()和Queue()类似测试之间的拖拽竞赛的时间结果......这是在运行Ubuntu 11.10和Python 2.7.2的ThinkpadT61上。

仅供参考,我将JoinableQueue()结果作为奖励投入; JoinableQueue()在queue.task_done()时会queue.task_done()任务(它甚至不知道特定任务,它只计算队列中未完成的任务),因此queue.join()知道工作已完成。

这个答案底部的每个代码......

mpenning@mpenning-T61:~$ python multi_pipe.py

Sending 10000 numbers to Pipe() took 0.0369849205017 seconds

Sending 100000 numbers to Pipe() took 0.328398942947 seconds

Sending 1000000 numbers to Pipe() took 3.17266988754 seconds

mpenning@mpenning-T61:~$ python multi_queue.py

Sending 10000 numbers to Queue() took 0.105256080627 seconds

Sending 100000 numbers to Queue() took 0.980564117432 seconds

Sending 1000000 numbers to Queue() took 10.1611330509 seconds

mpnening@mpenning-T61:~$ python multi_joinablequeue.py

Sending 10000 numbers to JoinableQueue() took 0.172781944275 seconds

Sending 100000 numbers to JoinableQueue() took 1.5714070797 seconds

Sending 1000000 numbers to JoinableQueue() took 15.8527247906 seconds

mpenning@mpenning-T61:~$

总之, Pipe()比Queue()快三倍。 除非你真的必须有好处,否则不要考虑JoinableQueue() 。

奖金材料2

多处理引入了信息流的细微变化,除非您知道一些快捷方式,否则会使调试变得困难。 例如,在许多条件下通过字典索引时,您可能有一个可正常工作的脚本,但在某些输入中很少失败。

通常我们会在整个python进程崩溃时找到失败的线索; 但是,如果多处理功能崩溃,则不会将未经请求的崩溃回溯打印到控制台。 追踪未知的多处理崩溃是很困难的,没有一个线索来解决崩溃的过程。

我发现追踪多处理崩溃信息的最简单方法是将整个多处理函数包装在try / except并使用traceback.print_exc() :

import traceback

def reader(args):

try:

# Insert stuff to be multiprocessed here

return args[0]['that']

except:

print "FATAL: reader({0}) exited while multiprocessing".format(args)

traceback.print_exc()

现在,当您发现崩溃时,您会看到以下内容:

FATAL: reader([{'crash', 'this'}]) exited while multiprocessing

Traceback (most recent call last):

File "foo.py", line 19, in __init__

self.run(task_q, result_q)

File "foo.py", line 46, in run

raise ValueError

ValueError

源代码:

"""

multi_pipe.py

"""

from multiprocessing import Process, Pipe

import time

def reader_proc(pipe):

## Read from the pipe; this will be spawned as a separate Process

p_output, p_input = pipe

p_input.close() # We are only reading

while True:

msg = p_output.recv() # Read from the output pipe and do nothing

if msg=='DONE':

break

def writer(count, p_input):

for ii in xrange(0, count):

p_input.send(ii) # Write 'count' numbers into the input pipe

p_input.send('DONE')

if __name__=='__main__':

for count in [10**4, 10**5, 10**6]:

# Pipes are unidirectional with two endpoints: p_input ------> p_output

p_output, p_input = Pipe() # writer() writes to p_input from _this_ process

reader_p = Process(target=reader_proc, args=((p_output, p_input),))

reader_p.daemon = True

reader_p.start() # Launch the reader process

p_output.close() # We no longer need this part of the Pipe()

_start = time.time()

writer(count, p_input) # Send a lot of stuff to reader_proc()

p_input.close()

reader_p.join()

print("Sending {0} numbers to Pipe() took {1} seconds".format(count,

(time.time() - _start)))

"""

multi_queue.py

"""

from multiprocessing import Process, Queue

import time

import sys

def reader_proc(queue):

## Read from the queue; this will be spawned as a separate Process

while True:

msg = queue.get() # Read from the queue and do nothing

if (msg == 'DONE'):

break

def writer(count, queue):

## Write to the queue

for ii in range(0, count):

queue.put(ii) # Write 'count' numbers into the queue

queue.put('DONE')

if __name__=='__main__':

pqueue = Queue() # writer() writes to pqueue from _this_ process

for count in [10**4, 10**5, 10**6]:

### reader_proc() reads from pqueue as a separate process

reader_p = Process(target=reader_proc, args=((pqueue),))

reader_p.daemon = True

reader_p.start() # Launch reader_proc() as a separate python process

_start = time.time()

writer(count, pqueue) # Send a lot of stuff to reader()

reader_p.join() # Wait for the reader to finish

print("Sending {0} numbers to Queue() took {1} seconds".format(count,

(time.time() - _start)))

"""

multi_joinablequeue.py

"""

from multiprocessing import Process, JoinableQueue

import time

def reader_proc(queue):

## Read from the queue; this will be spawned as a separate Process

while True:

msg = queue.get() # Read from the queue and do nothing

queue.task_done()

def writer(count, queue):

for ii in xrange(0, count):

queue.put(ii) # Write 'count' numbers into the queue

if __name__=='__main__':

for count in [10**4, 10**5, 10**6]:

jqueue = JoinableQueue() # writer() writes to jqueue from _this_ process

# reader_proc() reads from jqueue as a different process...

reader_p = Process(target=reader_proc, args=((jqueue),))

reader_p.daemon = True

reader_p.start() # Launch the reader process

_start = time.time()

writer(count, jqueue) # Send a lot of stuff to reader_proc() (in different process)

jqueue.join() # Wait for the reader to finish

print("Sending {0} numbers to JoinableQueue() took {1} seconds".format(count,

(time.time() - _start)))

python 管道、队列_python相关推荐

  1. python 管道队列_Python多处理 – 管道与队列

    > A Pipe()只能有两个端点. > A Queue()可以有多个生产者和消费者. 什么时候使用它们 如果你需要两点以上的沟通,使用Queue(). 如果你需要绝对的性能,Pipe() ...

  2. python有序队列_Python 队列

    所谓队列 队列是有序集合,添加操作发生在"尾部",移除操作则发生在"头部". 新元素从尾部进入 队列,然后一直向前移动到头部,直到成为下一个被移除的元素. 新添 ...

  3. python管道界面_python中管道用法入门实例

    本文实例讲述了python中管道用法.分享给大家供大家参考.具体如下: #!coding=utf-8 import multiprocessing def consumer(pipe): output ...

  4. python 管道队列_20.2、python进程间通信——队列和管道

    进程间通信--队列和管道(multiprocess.Queue.multiprocess.Pipe) 进程间通信 IPC(Inter-Process Communication) 队列 概念介绍 创建 ...

  5. python 管道队列_关于python:Multiprocessing-管道与队列

    Python的多处理程序包中的队列和管道之间的根本区别是什么? 在什么情况下应该选择一种? 什么时候使用Pipe()有优势? 什么时候使用Queue()有优势? Pipe()只能有两个端点. Queu ...

  6. python管道通信_Python进程通信之匿名管道实例讲解

    匿名管道 管道是一个单向通道,有点类似共享内存缓存.管道有两端,包括输入端和输出端.对于一个进程的而言,它只能看到管道一端,即要么是输入端要么是输出端. os.pipe()返回2个文件描述符(r, w ...

  7. python实现队列_Python学习教程:用队列实现栈

    接着上一期跟大家说的用栈实现队列,这期的 Python学习教程 跟大家讲 用队列实现栈 题目: 使用队列实现栈的下列操作: push(x) – 元素 x 入栈 pop() – 移除栈顶元素 top() ...

  8. python环形队列_Python 实现数据结构-循环队列的操作方法

    今天我们来到了循环队列这一节,之前的文章中,我介绍过了用python自带的列表来实现队列,这是最简单的实现方法. 但是,我们都知道,在列表中删除第一个元素和删除最后一个元素花费的时间代价是不一样的,删 ...

  9. python消息队列_python之消息队列

    消息持久化 acknowledgment 消息不丢失的方法 生效方法:channel.basic_consume(consumer_callback, queue, no_ack=False, exc ...

  10. python 清空队列_python实现:队列

    队列 队列和栈比较类似,可以和博文:https://blog.csdn.net/weixin_42521211/article/details/88946748 参考学习. 队列的一个特性是:先进先出 ...

最新文章

  1. Java的Exception和Error面试题10问10答
  2. NeHe教程Qt实现——lesson03
  3. IPC之哲学家进餐问题
  4. java clone 报错_创建ArrayList对象时显示CloneNotSupportedException错误
  5. python数据库操作nosql_用Python写一个NoSQL数据库
  6. OpenCV图像处理——深度学习样本制造
  7. java基础提升篇:Java 序列化的高级认识
  8. linux 查redis状态_干货:用案例代码详解Redis中的事件驱动模型
  9. 基于docker 如何部署surging分布式微服务引擎
  10. 5种样式实现div容器中三图摆放实例对比说明
  11. 有关“数据统计”的一些概念 -- PV UV VV IP跳出率等
  12. SQLSERVER查看阻塞和CPU占用的会话,存储过程执行性能排行
  13. 一流程序员都有哪些高效编程习惯?
  14. 乐鑫Esp32-S2学习之旅② ESP32-S2 以 I2C 驱动 SHT20 获取温湿度数据,代码开源!
  15. 计算机中英汉字段如何切换,电脑上中英文切换按哪个键
  16. 房屋管理系统简单Damo
  17. Cinder调度之Filter介绍
  18. 葫芦娃用计算机,葫芦娃互通版电脑版
  19. Asset Pricing:Introduction
  20. 家庭玩游戏配置哪种计算机,玩游戏用什么系统?Win10专业版和Win10家庭版玩游戏的区别...

热门文章

  1. Microsoft Virtual Academy 提取视频字幕
  2. IDEA如何上传java项目到github仓库里
  3. 六轴机械臂DIY(四)机械模型重建及DH法建模
  4. 【python】pop()栈函数介绍
  5. springMVC类型转换
  6. HbuilderX连接手机模拟器实战记录
  7. Elasticsearch集群搭建、优化及实践
  8. SpringBoot:yaml配置注入bean
  9. 7-93 藏尾诗 (20 分)
  10. GPS载频信号简述(L1、L2、L3、L5)