多进程是否支持命名管道(FIFO)?
Does multiprocessing support named pipes (FIFO)?
Multiprocessing的Pipes和Queue是基于匿名管道的,Python的multiprocessing
是否提供命名管道(先进先出)?
在 multiprocessing
中没有对命名管道的跨平台抽象的内置支持。
如果你只关心 Unix,或者只关心 Windows,你当然可以手动创建命名管道。对于 Unix,mkfifo
在标准库中。对于 Windows,您必须使用 ctypes
或 cffi
,或像 win32api
这样的第三方库来使用正确的参数调用 CreateFile
。
试图对两者之间的语义差异进行抽象是非常痛苦的,这可能是标准库不尝试这样做的原因。 (例如,Windows 命名管道是可变的;posix 命名管道是永久的。)
这是一个简单的 Unix 示例:
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()
class multiprocessing.connection.Listener([address[, family[, backlog[, authkey]]]])
A wrapper for a bound socket or Windows named pipe which is ‘listening’ for connections.address is the address to be used by the bound socket or named pipe of the listener object.
Multiprocessing的Pipes和Queue是基于匿名管道的,Python的multiprocessing
是否提供命名管道(先进先出)?
在 multiprocessing
中没有对命名管道的跨平台抽象的内置支持。
如果你只关心 Unix,或者只关心 Windows,你当然可以手动创建命名管道。对于 Unix,mkfifo
在标准库中。对于 Windows,您必须使用 ctypes
或 cffi
,或像 win32api
这样的第三方库来使用正确的参数调用 CreateFile
。
试图对两者之间的语义差异进行抽象是非常痛苦的,这可能是标准库不尝试这样做的原因。 (例如,Windows 命名管道是可变的;posix 命名管道是永久的。)
这是一个简单的 Unix 示例:
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()
class multiprocessing.connection.Listener([address[, family[, backlog[, authkey]]]])
A wrapper for a bound socket or Windows named pipe which is ‘listening’ for connections.address is the address to be used by the bound socket or named pipe of the listener object.