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.

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