proc1.py 817 B

1234567891011121314151617181920212223242526272829
  1. #!/usr/bin/env python
  2. from multiprocessing import Process
  3. import os
  4. import time
  5. import random
  6. def sleeper():
  7. numsecs = random.randrange(5) + 1
  8. print "[%s] Starting to sleep for %s seconds" % (os.getpid(), numsecs)
  9. time.sleep(numsecs)
  10. print "[%s] Done sleeping" % os.getpid()
  11. if __name__ == '__main__':
  12. print "[%s] parent start" % os.getpid()
  13. children = [Process(target=sleeper) for x in range(5)]
  14. print "[%s] children created but not spawned" % os.getpid()
  15. for child in children:
  16. print "[%s] spawning a child" % os.getpid()
  17. child.start()
  18. print "[%s] all children spawned" % os.getpid()
  19. for child in children:
  20. child.join()
  21. print "[%s] parent joined child [%s]" % (os.getpid(), child.pid)
  22. print "[%s] all children joined" % os.getpid()