How to send a var to stdin of an external software
Bryan Olson
fakeaddress at nowhere.org
Fri Mar 21 04:55:19 EDT 2008
Benjamin Watine wrote:
> bryanjugglercryptographer at yahoo.com a écrit :
>> 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."
>
> Great, it works, thank you Bryan !
>
> Could you explain me why you use a queue instead of a simple array for
> getting the piped var ?
The call to q.get() will block until an item is in the queue.
At that point in the program, we had already waited for cat to
terminate:
cat.wait()
myNewVar = q.get()
But passing cat.wait() does not imply that our own thread has
already read all of cat's output and put it in some destination
object. Data could still be in transit.
My first version, subsequently titled, "Doh! Race condition,"
worked in all of several runs of its built-in test. Doesn't
make it right.
--
--Bryan
More information about the Python-list
mailing list