最近在研究Python中的并行编程工具。这是 os.pipe 和 multiprocessing.Pipe 之间的两个主要区别。(尽管使用它们的场合)
- os.pipe 是单向的,multiprocessing.Pipe 是双向的;
- 当将东西放入管道/从管道接收东西时, os.pipe 使用encode/decode,而 multiprocessing.Pipe 使用pickle/unpickle
我想知道我的理解是否正确,还有其他区别吗?谢谢你。
最近在研究Python中的并行编程工具。这是 os.pipe 和 multiprocessing.Pipe 之间的两个主要区别。(尽管使用它们的场合)
我想知道我的理解是否正确,还有其他区别吗?谢谢你。
我相信你所说的一切都是正确的。
On Linux, os.pipe
is just a Python interface for accessing traditional POSIX pipes. On Windows, it's implemented using 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.
multiprocessing.Pipe
objects are much more high level interface, implemented using multiprocessing.Connection
objects. On Linux, these are actually built on top of POSIX sockets, rather than POSIX pipes. On Windows, they're built using the 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. They're capable of being both bidirectional and unidirectional.