def __enter__(self): # Teach the first command to take input specially self.commands[0].stdinSet = self.in_fd last_command = self.commands[0] # Connect all interior commands to one another via stdin/stdout for command in self.commands[1:]: last_command.start() # Set large kernel buffering between pipeline # participants. pipebuf.set_buf_size(last_command.stdout.fileno()) command.stdinSet = last_command.stdout last_command = command # Teach the last command to spill output to out_fd rather than to # its default, which is typically stdout. assert last_command is self.commands[-1] last_command.stdoutSet = self.out_fd last_command.start() stdin = self.commands[0].stdin if stdin is not None: self.stdin = pipebuf.NonBlockBufferedWriter(stdin) else: self.stdin = None stdout = self.commands[-1].stdout if stdout is not None: self.stdout = pipebuf.NonBlockBufferedReader(stdout) else: self.stdout = None return self
def popen_nonblock(*args, **kwargs): """ Create a process in the same way as popen_sp, but patch the file descriptors so they can be accessed from Python/gevent in a non-blocking manner. """ proc = popen_sp(*args, **kwargs) if proc.stdin: proc.stdin = pipebuf.NonBlockBufferedWriter(proc.stdin) if proc.stdout: proc.stdout = pipebuf.NonBlockBufferedReader(proc.stdout) if proc.stderr: proc.stderr = pipebuf.NonBlockBufferedReader(proc.stderr) return proc
def __enter__(self): # Ensure there is at least one step in the pipeline. # # If all optional features are turned off, the pipeline will # be completely empty and crash. if len(self.commands) == 0: self.commands.append(CatFilter()) # Teach the first command to take input specially self.commands[0].stdinSet = self.in_fd last_command = self.commands[0] # Connect all interior commands to one another via stdin/stdout for command in self.commands[1:]: last_command.start() # Set large kernel buffering between pipeline # participants. pipebuf.set_buf_size(last_command.stdout.fileno()) command.stdinSet = last_command.stdout last_command = command # Teach the last command to spill output to out_fd rather than to # its default, which is typically stdout. assert last_command is self.commands[-1] last_command.stdoutSet = self.out_fd last_command.start() stdin = self.commands[0].stdin if stdin is not None: self.stdin = pipebuf.NonBlockBufferedWriter(stdin) else: self.stdin = None stdout = self.commands[-1].stdout if stdout is not None: self.stdout = pipebuf.NonBlockBufferedReader(stdout) else: self.stdout = None return self