pipes and popen2

Thomas Wouters thomas at xs4all.net
Tue Jun 13 06:05:20 EDT 2000


On Tue, Jun 13, 2000 at 08:08:47AM +0000, Stian Husemoen wrote:

> I was a bit bored this weekend, and passed time coding python.

If only you had spent a little time reading the newsgroup as well ! ;-)

> In one snippet I wanted to make a UI to ispell. The idea was to create a
> bidirectional pipe, keep that pipe open, and feed ispell with words to
> spell. Like in this pseudo-code:

> 	stdout, stdin = popen2.popen2('ispell -a')
> 	
> 	for word in wordlist:
> 		# spell this
> 		stdin.write('pyhton')
> 
> 		# result
> 		print stdin.readline()
> 
> Problem is popen2 wont give me any output until I close stdin. Is there any
> way to both read and write to a program while keeping the pipe and
> process open? I can't seem to find any postings or documentation on this
> subject.

Curious, as I replied something very similar to someone else, only yesterday
;-) The solution is to set the buffersize of the pipe, by passing the
optional 2nd argument to popen2:

stdout, stdin = popen2.popen2('ispell -a', 1) # or something similarly small

So that the read*() methods don't stop to wait for more input.

-- 
Thomas Wouters <thomas at xs4all.net>

Hi! I'm a .signature virus! copy me into your .signature file to help me spread!




More information about the Python-list mailing list