简体   繁体   English

Python os.pipe与multiprocessing.Pipe

[英]Python os.pipe vs multiprocessing.Pipe

Recently I'm studying parallel programming tools in Python. 最近我正在研究Python中的并行编程工具。 And here are two major differences between os.pipe and multiprocessing.Pipe.(despite the occasion they are used) 这是os.pipe和multiprocessing.Pipe之间的两个主要区别。(尽管它们被使用)

  1. os.pipe is unidirectional , multiprocessing.Pipe is bidirectional ; os.pipe是单向的 ,多处理。管道是双向的 ;
  2. When putting things into pipe/receive things from pipe, os.pipe uses encode/decode , while multiprocessing.Pipe uses pickle/unpickle 当把东西放入管道/从管道接收东西时,os.pipe使用编码/解码 ,而多处理。管道使用pickle / unpickle

I want to know if my understanding is correct, and is there other difference? 我想知道我的理解是否正确,还有其他区别吗? Thank you. 谢谢。

I believe everything you've stated is correct. 我相信你所陈述的一切都是正确的。

On Linux, os.pipe is just a Python interface for accessing traditional POSIX pipes. 在Linux上, os.pipe只是一个用于访问传统POSIX管道的Python接口。 On Windows, it's implemented using CreatePipe . 在Windows上,它是使用CreatePipe实现的。 When you call it, you get two ordinary file descriptors back. 当你调用它时,你会得到两个普通的文件描述符。 It's unidirectional, and you just write bytes to it on one end that get buffered by the kernel until someone reads from the other side. 它是单向的,你只需在一端写入字节,由内核缓冲,直到有人从另一端读取。 It's fairly low-level, at least by Python standards. 这是相当低级的,至少是Python标准。

multiprocessing.Pipe objects are much more high level interface, implemented using multiprocessing.Connection objects. multiprocessing.Pipe对象是更高级的接口,使用multiprocessing.Connection实现。连接对象。 On Linux, these are actually built on top of POSIX sockets, rather than POSIX pipes. 在Linux上,这些实际上是建立在POSIX套接字之上,而不是POSIX管道。 On Windows, they're built using the CreateNamedPipe API. 在Windows上,它们是使用CreateNamedPipe API构建的。 As you noted, multiprocessing.Connection objects can send/receive any picklable object, and will automatically handle the pickling/unpickling process, rather than just dealing with bytes. 如你所述, multiprocessing.Connection对象可以发送/接收任何可选对象,并自动处理pickle / unpickling进程,而不仅仅是处理字节。 They're capable of being both bidirectional and unidirectional. 它们既可以是双向的,也可以是单向的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM