Multiple Processes on Unix with Python

Donn Cave donn at oz.net
Thu Mar 22 23:10:49 EST 2001


Quoth Nickson Fong <nickson at centropolisfx.com>:
| Can anyone show me a quick example on how to fork multiple processes on
| a multiple cpu Irix machine. I know I need os.fork() and os.execvp() but
| can't figure out how they work together.

The reason it's so hard, is that there are so many different things
you might want to do.  One thing you might be missing is os.waitpid(),
but it's hard to say.

Just for something completely different, take a look at this:

    import os

    p = []
    p.append(os.spawnv(os.P_NOWAIT, '/bin/date', ('date',)))
    p.append(os.spawnv(os.P_NOWAIT, '/bin/date', ('date',)))
    p.append(os.spawnv(os.P_NOWAIT, '/bin/date', ('date',)))

    while p:
        for i in range(len(p)):
            pi, st = os.waitpid(p[i], os.WNOHANG)
            if pi == p[i]:
                del p[i]
                print 'pid', pi, 'status', st
                break

	Donn Cave, donn at oz.net



More information about the Python-list mailing list