def spawn_child(self, conn=None): parent_fd, child_fd = passfd.socketpair(socket.AF_UNIX, socket.SOCK_STREAM) # make child fd non-blocking flags = fcntl.fcntl(child_fd, fcntl.F_GETFL, 0) fcntl.fcntl(child_fd, fcntl.F_SETFL, flags | os.O_NONBLOCK) pid = os.fork() if pid == 0: if conn: conn.close() # in the midst of handling a request, close # the connection in the child os.close(child_fd) self.handler_class(parent_fd).serve() sys.exit(0) else: os.close(parent_fd) self.children.append(Child(pid, child_fd))
def spawn_child(self, session_id, conn=None): parent_fd, child_fd = passfd.socketpair(socket.AF_UNIX, socket.SOCK_STREAM) # make child fd non-blocking flags = fcntl.fcntl(child_fd, fcntl.F_GETFL, 0) fcntl.fcntl(child_fd, fcntl.F_SETFL, flags | os.O_NONBLOCK) pid = os.fork() if pid == 0: if conn is not None: conn.close() # in the midst of handling a request, close # the connection in the child os.close(child_fd) self.socket.close() for child in self.children.values(): child.close() self.handler_class(parent_fd).serve() sys.exit(0) else: os.close(parent_fd) self.children[session_id] = child = Child(session_id, pid, child_fd) log('started child (session=%s pid=%s nchild=%s)' % (session_id, pid, len(self.children))) return child
#!/usr/bin/env python3 # import os, sys, socket from scgi import passfd import tempfile # # Create a pipe for sending the fd. # rfd, wfd = passfd.socketpair(socket.AF_UNIX, socket.SOCK_STREAM) print("rfd", rfd, "wfd", wfd) # We will pass this to the child fileObj = tempfile.TemporaryFile() line = b'Hello world!\n' fileObj.write(line) fileObj.flush() fileObj.seek(0) # # fork() off! # pid = os.fork() if pid != 0: # We're in the parent. # ioctl() will only pass raw filedescriptors. Find fd of fileObj.