How to use popen ?
donn at oz.net
Sat May 5 06:12:53 CEST 2001
Quoth harvey smith <harvey at tunes.norfolk.street>:
| I'd like to be able to start another process and send (write to it)
| commands. I want the other processes output to be unaffected, as i
| don't need to process the output from it in any way, i just need to
| see it.
| As a test to figure this out I try th following: (running dc in rxvt
| and trying to add a couple numbers and see results)
| :import os
| :fp = os.popen('rxvt -e dc','w')
| :fp.write('12 3+p')
| :print "all done"
| Though an rxvt term opens just fine, running dc as expected, the
| write, "12 3+p", never shows up. Once I exit out of dc "all done" prints and no
| errors are displayed.
It is not trivial to get something like this to work. rxvt allocates
a "pseudotty" device, and the tty side of it is the input and output
for "dc". (The other side goes to rxvt, to be converted to X events.)
rxvt ignores its own input.
I don't have rxvt or any X at all at hand, at the moment, to try this
with, but I can think of three general possibilities. First: if rxvt
(or xterm or whatever) actually supports this as a feature, via some
option, then of course read up on it and use that option.
Second: if you have write access to dc's tty, write its input there.
(You will need some way to find that out - maybe run dc from a script
that first runs "tty".)
Third: open a pipe, find out its read unit number and at run time modify
the "dc" command to redirect input from that unit, hoping that rxvt
allows all its file descriptors to pass through to the "dc" fork;
don't use popen, just os.pipe() and os.spawnv(). Hm, fourth option
along the same lines, use a named pipe and redirect from that name -
really that might be the most sensible approach.
Donn Cave, donn at oz.net
More information about the Python-list