How to send a var to stdin of an external software

bryanjugglercryptographer at bryanjugglercryptographer at
Fri Mar 14 23:06:44 CET 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):

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

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

    box = []
    thread.start_new_thread(readtobox, (cat.stdout, box))
    myNewVar = box[0]

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


More information about the Python-list mailing list