1234567891011121314151617181920212223242526272829 |
- #!/usr/bin/env python
- from multiprocessing import Process
- import os
- import time
- import random
- def sleeper():
- numsecs = random.randrange(5) + 1
- print "[%s] Starting to sleep for %s seconds" % (os.getpid(), numsecs)
- time.sleep(numsecs)
- print "[%s] Done sleeping" % os.getpid()
- if __name__ == '__main__':
- print "[%s] parent start" % os.getpid()
- children = [Process(target=sleeper) for x in range(5)]
- print "[%s] children created but not spawned" % os.getpid()
- for child in children:
- print "[%s] spawning a child" % os.getpid()
- child.start()
- print "[%s] all children spawned" % os.getpid()
- for child in children:
- child.join()
- print "[%s] parent joined child [%s]" % (os.getpid(), child.pid)
- print "[%s] all children joined" % os.getpid()
|