Do a fork-exec through the subprocess.Popen abstraction in a way
    that takes a stdin and return stdout.
964409
def fork_exec(args, stdin='', **kwargs):
    

    as_bytes = isinstance(stdin, bytes)
    source = stdin if as_bytes else stdin.encode(locale)
    p = Popen(args, stdin=PIPE, stdout=PIPE, stderr=PIPE, **kwargs)
    stdout, stderr = p.communicate(source)
    if as_bytes:
        return stdout, stderr
    return (stdout.decode(locale), stderr.decode(locale))
8693
def execute(cmd):
    """"""
    proc = Popen(cmd, stdout=PIPE)
    stdout, _ = proc.communicate()
    if proc.returncode != 0:
        raise CalledProcessError(proc.returncode, " ".join(cmd))

    return stdout.decode('utf8')
782074
def run_command_for_code(command):
    
    process = subprocess.Popen(
        command, stdout=subprocess.PIPE, stderr=subprocess.PIPE
    )
    process.communicate()
    exit_code = process.returncode
    return exit_code
155154
def _exec_command(self, command: str):
        
        stdin, stdout, stderr = self._ssh.exec_command(command)
        # Clearing the buffers
        stdout.read()
        stderr.read()
        stdin.close()
1060488
def capture(command, input=None, cwd=None, shell=False, raiseOnError=False):
		
		
		# Attempt to execute the child process
		proc = subprocess.Popen(command, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=cwd, shell=shell, universal_newlines=True)
		(stdout, stderr) = proc.communicate(input)
		
		# If the child process failed and we were asked to raise an exception, do so
		if raiseOnError == True and proc.returncode != 0:
			raise Exception(
				'child process ' + str(command) +
				' failed with exit code ' + str(proc.returncode) +
				'\nstdout: "' + stdout + '"' +
				'\nstderr: "' + stderr + '"'
			)
		
		return CommandOutput(proc.returncode, stdout, stderr)
115173
def shell(self, command, *args, environment=None):
        
        command += ' ' + ' '.join(args)
        command = command.strip()
        self.debug(self.yellow_style('$ %s' % command))
        env = self.env.copy()
        env.update(environment or {})
        return subprocess.call(command, shell=True, env=env)
1266986
def start(self, stdout=subprocess.PIPE, stderr=subprocess.PIPE):
        
        if self.pre_start_check():
            # Some other executor (or process) is running with same config:
            raise AlreadyRunning(self)

        if self.process is None:
            command = self.command
            if not self._shell:
                command = self.command_parts

            env = os.environ.copy()
            env[ENV_UUID] = self._uuid
            popen_kwargs = {
                'shell': self._shell,
                'stdin': subprocess.PIPE,
                'stdout': stdout,
                'stderr': stderr,
                'universal_newlines': True,
                'env': env,
            }
            if platform.system() != 'Windows':
                popen_kwargs['preexec_fn'] = os.setsid
            self.process = subprocess.Popen(
                command,
                **popen_kwargs,
            )

        self._set_timeout()

        self.wait_for(self.check_subprocess)
        return self
164450
def run_at_subprocess(self, use_subprocess, foo, *args, **kwrags):
        
        if use_subprocess is False:
            return foo(*args, **kwrags)

        child_pid = os.fork()
        if child_pid == 0:
            foo(*args, **kwrags)
            sys.exit(0)
        return os.waitpid(child_pid, 0)[1] == 0
1098447
def subprocess(cls, cmd, **kwargs):
    
    def call(args):
      return subprocess.call(cmd + args, **kwargs)
    return cls(call)
887076
def execute(scope, command):
    
    process = Popen(command[0],
                    shell=True,
                    stdin=PIPE,
                    stdout=PIPE,
                    stderr=STDOUT,
                    close_fds=True)
    scope.define(__response__=process.stdout.read())
    return True
503328
def popen(*args, **kwargs):
    
    process = subprocess.Popen(*args, **kwargs)
    try:
        yield process.pid
    finally:
        os.kill(process.pid, signal.SIGTERM)
        os.waitpid(process.pid, 0)
207492
def Popen(cmd, stdin=None, stdout=PIPE, debug=False, shell="/bin/bash"):
    
    from subprocess import Popen as P
    if debug:
        logging.debug(cmd)
    # See: <https://blog.nelhage.com/2010/02/a-very-subtle-bug/>
    proc = P(cmd, bufsize=1, stdin=stdin, stdout=stdout, \
             shell=True, executable=shell)
    return proc
1371951
def run(command, input=None, exit=0, shell=False):
        
        if isinstance(input, str):
            input = input.encode()

        # Only pipe stdin if we have input to pipe.
        stdin = {} if input is None else {"stdin": subprocess.PIPE}
        try:
            child = subprocess.Popen(command, stdout=subprocess.PIPE,
                                     stderr=subprocess.PIPE, **stdin)
        except FileNotFoundError as e:
            # Extract name of command.
            name = command.split(' ', 1)[0] if isinstance(command, str) else command[0]
            raise DependencyError(name)

        stdout, _ = child.communicate(input=input)
        if exit is not None and child.returncode != exit:
            raise Error("failed to stylecheck code")
        return stdout.decode()
1141103
def execute &block
      raise 'Previous process still exists' unless pipes.empty?

      # clear callbacks
      @callbacks = []
      @errbacks = []

      pid, stdin, stdout, stderr = POSIX::Spawn.popen4 @command.to_s

      @pid = pid
      @stdin  = attach_pipe_handler :stdin, stdin
      @stdout = attach_pipe_handler :stdout, stdout
      @stderr = attach_pipe_handler :stderr, stderr

      if block
        block.call self
      elsif @execution_proc
        @execution_proc.call self
      end
      self
    end
1021318
def execute(self, command):
        """"""
        process = subprocess.Popen(
            command,
            preexec_fn=lambda: signal.signal(signal.SIGPIPE, signal.SIG_DFL)
        )
        return process
1195896
async def execute_process(*cmd, log=None, loop=None):
    '''
    

    '''
    p = await asyncio.create_subprocess_exec(
        *cmd,
        stdin=asyncio.subprocess.PIPE,
        stdout=asyncio.subprocess.PIPE,
        stderr=asyncio.subprocess.PIPE,
        loop=loop)
    stdout, stderr = await p.communicate()
    if log:
        log.debug("Exec %s -> %d", cmd, p.returncode)
        if stdout:
            log.debug(stdout.decode('utf-8'))
        if stderr:
            log.debug(stderr.decode('utf-8'))
    return p.returncode == 0
1321803
def run_command(args, cwd = None, shell = False, timeout = None, env = None):
    '''
    
    '''
    from subprocess import PIPE, Popen
    from StringIO import StringIO
    import fcntl, os, signal
    p = Popen(args, shell = shell, cwd = cwd, stdout = PIPE, stderr = PIPE, env = env)
    tstart = time.time()
    buf = StringIO()

    # try to make it non-blocking
    try:
        fcntl.fcntl(p.stdout, fcntl.F_SETFL, fcntl.fcntl(p.stdout, fcntl.F_GETFL) | os.O_NONBLOCK)
    except Exception:
        pass

    while True:
        time.sleep(0.1)
        retcode = p.poll()
        try:
            buf.write(p.stdout.read())
        except Exception:
            pass
        if retcode is not None:
            break
        if timeout is not None and time.time() > tstart + timeout:
            print("timeout in process %u" % p.pid)
            try: 
                os.kill(p.pid, signal.SIGKILL)
            except OSError:
                pass
            p.wait()
    return buf.getvalue()