Using Pipes Between Python & C++ Processes

Vijayan Sarathy vp at cfdrc.com
Fri Nov 9 21:54:32 CET 2001


Hello,

I am trying to make a Python script spawn a C++ child process and then
have the two of them communicate with each other using pipes.I am trying
to have the child process (C++)  write something and have the parent
(Python) read it. I hand off the file descriptor for the write-end of
the pipe to the child process through its argv list. The C++ child
process is not able to write to the pipe. When I check on errno, it
turns out to be EBADF (invalid file descriptor).  This problem occurs
ONLY on Windows. It works well on Linux . I am using Python 2.1

Could someone please enlighten me about what I am doing wrong.
Thanks.
Sincerely
Vijayan Sarathy


 I have included the Python function that does the job of spawning the
child using fork and execv. and returns the PID of the child process



# Fork a Child Process.
# Open a Pipe to the Child and Read what the Child Writes to it
def spawnModule (commandString):

    (r,w)= os.pipe()
    pid= os.fork()

    if (pid < 0):
        print "Error in Forking"
        sys.exit (-1);

    elif (pid == 0):

        # Child Process
        os.close (r)
        writeFD= "%d"%w
        args=[]
        args.append (commandString)
        args.append (writeFD)
        os.execv (args[0],args)

    else:

        # Parent Process
        os.close (w)
        nbytes= 1024
        string= os.read (r,nbytes)
        print "Received This String -> ", string
        os.close (r)

    return pid






More information about the Python-list mailing list