win32pipe.popen4 howto example

yaipa yaipa at yahoo.com
Wed Oct 29 22:25:59 CET 2008


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'




More information about the Python-list mailing list