newbie: how to capture/write to stdio on NT

S

Sholtz

Hi,

I am trying to figure out how to 'control' the input & output using
popen/popen2 etc on Python 2.3
I have found examples for unix such as the one below but I can't get it to
work on Windows NT.

If I use the os.popen module I can read OR write not both.

Anybody have any ideas?

Regards,

Sholto.

# Open command in a pipe
# which reads from stdin and writes to stdout

import popen2
pipe = popen2.Popen4("wc -l") # Unix command
pipe.tochild.write("line 1\nline 2\nline 3\n")
pipe.tochild.close()
output = pipe.fromchild.read()
 
A

A. Lloyd Flanagan

Sholtz said:
Hi,

I am trying to figure out how to 'control' the input & output using
popen/popen2 etc on Python 2.3
I have found examples for unix such as the one below but I can't get it to
work on Windows NT.

If I use the os.popen module I can read OR write not both.

Anybody have any ideas?

Regards,

Sholto.
popen() at least used to not work reliably under Windows, not sure if
that's the current status. There apparently were problems with the
POSIX emulation and the fact that the Windows methods for creating
processes are somewhat different. You might look into the
CreateProcess() system call.
 
A

Arnold Filip

Sholtz said:
Hi,

I am trying to figure out how to 'control' the input & output using
popen/popen2 etc on Python 2.3
I have found examples for unix such as the one below but I can't get it to
work on Windows NT.

If I use the os.popen module I can read OR write not both.

Anybody have any ideas?

Regards,

Sholto.

# Open command in a pipe
# which reads from stdin and writes to stdout

import popen2
pipe = popen2.Popen4("wc -l") # Unix command
pipe.tochild.write("line 1\nline 2\nline 3\n")
pipe.tochild.close()
output = pipe.fromchild.read()

I use the process.py module for this purpose:
http://starship.python.net/crew/tmick/

Example (works on windows):

import process
import sys

p = process.ProcessProxy( sys.argv[1:],
stdout=sys.stderr,
stderr=sys.stderr )
p.wait()

Cheers
Arnold
 
D

Duncan Booth

I am trying to figure out how to 'control' the input & output using
popen/popen2 etc on Python 2.3
I have found examples for unix such as the one below but I can't get
it to work on Windows NT.

If I use the os.popen module I can read OR write not both.


# Open command in a pipe
# which reads from stdin and writes to stdout

import popen2
pipe = popen2.Popen4("wc -l") # Unix command
pipe.tochild.write("line 1\nline 2\nline 3\n")
pipe.tochild.close()
output = pipe.fromchild.read()
As the documentation for popen2 says, the methods such as popen4 exist on
windows, but the classes don't. So you have to use:
line 1
line 2
line 3


Also be sure to read the section of the documentation about flow control
issues. If there can ever be more than 4k of output waiting to be read your
code will deadlock. If the program is trying to read input, then you won't
get any output until it has read enough or you close the input.

The simplest thing is to always be sure to read pipeout on a different
thread than you use to write to pipein. You cannot use 'select' on windows
pipes.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top