win32pipe.popen4 howto example

Y

yaipa

trying to remind myself that running a subprocess under Win32 isn't a
pain in the ass... ];^)

###########################################################
#!/usr/bin/env python

'''
original author
credits: chris levis

original sources found:
http://mail.python.org/pipermail/python-list/2005-August/336390.html

from python docs,
17.4 popen2 -- Subprocesses with accessible I/O streams

popen4(cmd [, bufsize[, mode]])
Executes cmd as a sub-process.
Returns the file objects (child_stdout_and_stderr,
child_stdin).
-- New in version 2.0.

If,
o bufsize, is provided, it specifies the buffer size for
the I/O pipes.
o mode, is provided, the string 'b' or 't';
- 'b' required on Windows for binary read access
- 't' (text mode) is default
'''

'''
External test file.

test_exit_code.py

---- start snippet ---------

import sys

print "hello, world"
sys.exit(-1)

---- end snippet -----------

'''

import win32pipe;

cmd = 'test_exit_code.py'

# open stdout pseudo file in textmode 't'
(stdin,stdout) = win32pipe.popen4( cmd, 't' );

stdin.close();
output = stdout.read();
try:
exitcode = stdout.close() or 0;
except IOError:
exitcode = ( -1 );

print output ## prints 'hello world'
print exitcode ## print '-1'
 

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

No members online now.

Forum statistics

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

Latest Threads

Top