How to send a var to stdin of an external software

Benjamin Watine watine at cines.fr
Mon Mar 17 14:19:10 CET 2008


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."
> 
> 
> --
> --Bryan
> 

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 ?

Regards,

Ben




More information about the Python-list mailing list