多处理的管道和队列基于匿名管道,Python的multiprocessing
是否提供命名管道(FIFO)?
There's no built-in support for a cross-platform abstraction of named pipes in multiprocessing
.
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.
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.)
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()
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.