Hi all,

I am have a function which executes a command in the shell. The stdout and stderr of the command should be multipled to two strings for stdout and stderr respectively and stdout and stderr of the current process respectively.

I have done like this:
from subprocess import Popen, PIPE, STDOUT
from select import select
from os import read
from sys import stdout, stderr

def communicate(p):
"""

Multiplex the subprocess stdout/stderr to the process stdout/stderr

and a tuple of strings

    """
    output = []
    errput = []

    while True:
        (ready_to_read, _, _) = select([p.stdout, p.stderr], [], [])
        dataout = ""
        dataerr = ""

        if p.stdout in ready_to_read:
            dataout = read(p.stdout.fileno(), 1024)
            stdout.write(dataout)
            output.append(dataout)

        if p.stderr in ready_to_read:
            dataerr = read(p.stderr.fileno(), 1024)
            stderr.write(dataerr)
            errput.append(dataerr)

        if dataout == "" and dataerr == "":
            p.stdout.close()
            p.stderr.close()
            break

    return (''.join(output), ''.join(errput))

def exe(s, cwd=None, output_command=True):
    if output_command:
        print s
    p = Popen(s, stdin=None, stdout=PIPE, stderr=PIPE, shell=True, cwd=cwd)
    (output, err) = communicate(p)
    rc = p.wait()
    return (rc, output, err)


Unfortunately, the program is _sometimes_ but not always getting stuck on the call to select. I don't really understand when this happens. Any suggestions to the above code so select doesn't block the function?

Cheers,

--
PMatos

--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to