How to send a var to stdin of an external software

bryanjugglercryptographer at yahoo.com bryanjugglercryptographer at yahoo.com
Fri Mar 14 18:33:09 EDT 2008


I wrote:
> And here's a thread example, based on Benjamin's code:
[...]

Doh! Race condition. Make that:

    import subprocess
    import thread
    import Queue

    def readtoq(pipe, q):
        q.put(pipe.read())

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

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

    q = Queue.Queue()
    thread.start_new_thread(readtoq, (cat.stdout, q))
    cat.stdin.write(myVar)
    cat.stdin.close()
    cat.wait()
    myNewVar = q.get()

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


--
--Bryan






More information about the Python-list mailing list