Fast way to send message between 2 child processes? (using a file is too slow?)
Jeff Epler
jepler at unpythonic.net
Mon Mar 8 22:25:05 EST 2004
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:
More information about the Python-list
mailing list