How to send a var to stdin of an external software

bryanjugglercryptographer at yahoo.com bryanjugglercryptographer at yahoo.com
Fri Mar 14 18:06:44 EDT 2008


Floris Bruynooghe wrote:
> Benjamin Watine wrote:
> > Could you give me more information / examples about the two solutions
> > you've proposed (thread or asynchronous I/O) ?
>
> The source code of the subprocess module shows how to do it with
> select IIRC.  Look at the implementation of the communicate() method.

And here's a thread example, based on Benjamin's code:

    import subprocess
    import thread

    def readtobox(pipe, box):
        box.append(pipe.read())

    cat = subprocess.Popen('cat', shell=True, stdin=subprocess.PIPE,
stdout=subprocess.PIPE)

    myVar = str(range(1000000)) # arbitrary test data.

    box = []
    thread.start_new_thread(readtobox, (cat.stdout, box))
    cat.stdin.write(myVar)
    cat.stdin.close()
    cat.wait()
    myNewVar = box[0]

    assert myNewVar == myVar
    print len(myNewVar), "bytes piped around."


--
--Bryan




More information about the Python-list mailing list