多处理程序支持命名管道(FIFO)吗? [英] Does multiprocessing support named pipes (FIFO)?
问题描述
多处理的 Pipes 和 Queue 基于匿名管道,Python的multiprocessing
是否提供命名管道(FIFO)?
Multiprocessing's Pipes and Queue are based on anonymous pipes, does the multiprocessing
of Python provide named pipes (FIFO)?
推荐答案
multiprocessing
中没有对命名管道的跨平台抽象的内置支持.
There's no built-in support for a cross-platform abstraction of named pipes in multiprocessing
.
如果仅关心Unix或仅关心Windows,那么当然可以手动创建命名管道.对于Unix,在stdlib中为 mkfifo
.对于Windows,您必须使用ctypes
或cffi
或第三方库(例如win32api
)来使用正确的参数调用CreateFile
.
If you only care about Unix, or only about Windows, you can of course create named pipes manually. For Unix, mkfifo
is in the stdlib. For Windows, you have to use ctypes
or cffi
, or a third-party library like win32api
to call CreateFile
with the right arguments.
尝试抽象两者之间的语义差异是很痛苦的,这也许就是为什么stdlib不尝试这样做的原因. (例如,Windows命名管道是易失的; posix命名管道是永久的.)
Trying to abstract over the semantic differences between the two is pretty painful, which is probably why the stdlib doesn't attempt to do so. (For example, Windows named pipes are volatile; posix named pipes are permanent.)
这是一个琐碎的Unix示例:
Here's a trivial Unix example:
import multiprocessing
import os
def child():
with open('mypipe', 'rb') as p:
print(p.read())
def main():
try:
os.mkfifo('mypipe')
except FileExistsError:
pass
multiprocessing.Process(target=child).start()
with open('mypipe', 'wb') as p:
p.write(b'hi')
os.remove('mypipe')
if __name__ == '__main__':
main()
这篇关于多处理程序支持命名管道(FIFO)吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!