Fast way to send message between 2 child processes? (using a file is too slow?)
jepler at unpythonic.net
Wed Mar 10 03:41:05 CET 2004
[Copied back to python-list in case anybody there is interested in this]
Jeff Epler wrote [part of the body for coroutine()]:
> > os.dup2(F(child_stdin), 0)
> > os.dup2(F(child_stdout), 1)
> > os.dup2(F(child_stderr), 2)
On Tue, Mar 09, 2004 at 05:56:09PM -0800, seberino at spawar.navy.mil wrote:
> What do these dup2 commands do??? What exactly is child process
> doing in this part? I assume this is were we do the trick of
> making different processes use same i/o objects?
Basically, this makes fd 0 correspond to child_stdin, etc. Unix programs
assume that fd 0 is stdin, fd 1 is stdout, and fd 2 is stderr. The
manpage for dup2() explains the exact behavior better than I could.
> > for j in range(3, 255):
> > try: os.close(j)
> > except: pass
> What is purpose of this part?
This closes any files open in the parent process which should not be
available in the child. Instead of the hardcoded value 255, it should
probably be os.sysconf("SC_OPEN_MAX"). (see the manpage for sysconf)
If you are writing the child programs, and would rather have
stdin/stdout/stderr behave normally in them, then you could use any fd
you want, changing coroutine() to leave that fd open instead of closing
it. You could have a convention that the pipe is on fd3, for instance,
for j in range(4, os.sysconf("SC_OPEN_MAX"):
.. if you don't have control over the child, then this approach will
probbaly not work for you. (incidentally, gimp uses a scheme like this
for "plugins", but I think it gives the fd number on the commandline.
gimp "plugins" run as separate processes from gimp itself, not as
dynamically-loaded shared libs or the like)
coroutine() is both more and less useful than popen: it lets you do more
things with file descriptors, but it doesn't automatically invoke the
shell or do anything special to clean up when the child processes exit.
I also don't know if you can write coroutine() on Windows, where
fork+exec is done in one step by os.spawnv.
More information about the Python-list