如何在python中将文件描述符从父传递给子?

kum*_*mar 6 python file-descriptor multiprocessing

我正在使用多处理模块,并使用池来启动多个工作程序.但是在父进程中打开的文件描述符在工作进程中关闭.我希望他们开放......!有没有办法传递文件描述符在父和子之间共享?

Pio*_*icz 7

在Python 2和Python 3上,multiprocessing.reduction模块中存在用于发送和接收文件描述符的函数.

示例代码(Python 2和Python 3):

import multiprocessing
import os

# Before fork
child_pipe, parent_pipe = multiprocessing.Pipe(duplex=True)

child_pid = os.fork()

if child_pid:
    # Inside parent process
    import multiprocessing.reduction
    import socket
    # has socket_to_pass socket object which want to pass to the child
    socket_to_pass = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
    socket_to_pass.connect("/dev/log")
    # child_pid argument to send_handle() can be arbitrary on Unix,
    # on Windows it has to be child PID
    multiprocessing.reduction.send_handle(parent_pipe, socket_to_pass.fileno(), child_pid)
    socket_to_pass.send("hello from the parent process\n".encode())
else:
    # Inside child process
    import multiprocessing.reduction
    import socket
    import os
    fd = multiprocessing.reduction.recv_handle(child_pipe)
    # rebuild the socket object from fd
    received_socket = socket.fromfd(fd, socket.AF_INET, socket.SOCK_STREAM)
    # socket.fromfd() duplicates fd, so we can close the received one
    os.close(fd)
    # and now you can communicate using the received socket
    received_socket.send("hello from the child process\n".encode())
Run Code Online (Sandbox Code Playgroud)


Mat*_*son -1

据我所知,没有一种方法可以在进程之间共享文件描述符。如果存在某种方法,它很可能是特定于操作系统的。

我的猜测是您需要在另一个层面上共享数据。

  • 同意。不过,有一些特定于操作系统的方法。 (4认同)