Fast way to send message between 2 child processes? (using a file is too slow?)

seberino at spawar.navy.mil seberino at spawar.navy.mil
Mon Mar 8 23:44:03 EST 2004


Jeff

Thanks.  What about sockets??  If we
hardwire child processes to have a fixed ip address
and port then I can communicate with a socket right?

Are sockets better or worse than your idea?  Why/Why not?

Chris

On Mon, Mar 08, 2004 at 09:25:05PM -0600, Jeff Epler wrote:
> You can use os.pipe() to get a pair of fds, where data written to the
> second one can be read by the first one.  Untested code:
>     reader, writer = os.pipe()
>
>     pid1 = coroutine(('/bin/sh', '-c', 'some code1'), \
>                     reader, sys.stdout, sys.stderr)
>     pid2 = coroutine(('/bin/sh', '-c', 'some code2'), \
>                     sys.stdin, writer, sys.stderr)
>
> Use os.waitpid() to reap the child processes.
>
> All of this assumes Unix, of course.
>
> Jeff
>
>
> ------------------------------------------------------------------------
> Definitions of coroutine() and supporting code.  Moderately tested:
>
> dev_null = open("/dev/null", "rw")
> def F(x):
>     if x is None: return dev_null.fileno()
>     if not isinstance(x, int): return x.fileno()
>     return x
>
> def coroutine(args, child_stdin, child_stdout, child_stderr):
>     pid = os.fork()
>     if pid == 0:
>         os.dup2(F(child_stdin), 0)
>         os.dup2(F(child_stdout), 1)
>         os.dup2(F(child_stderr), 2)
>         for j in range(3, 255):
>             try: os.close(j)
>             except: pass
>         os.execvp(args[0], args)
>         print "execvp failed ?"
>         os._exit(99)
>     return pid
>
> On Mon, Mar 08, 2004 at 06:27:33PM -0800, Christian Seberino wrote:
>

--
_______________________________________

Christian Seberino, Ph.D.
SPAWAR Systems Center San Diego
Code 2872
49258 Mills Street, Room 158
San Diego, CA 92152-5385
U.S.A.

Phone: (619) 553-9973
Fax  : (619) 553-6521
Email: seberino at spawar.navy.mil
_______________________________________




More information about the Python-list mailing list