Advertisement
Guest User

Untitled

a guest
May 22nd, 2015
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # encoding: utf-8
  3. #
  4. # Copyright (c) 2008 Doug Hellmann All rights reserved.
  5. #
  6. """Using a Condition to control sequencing between workers.
  7. """
  8. #end_pymotw_header
  9. import multiprocessing
  10. import time
  11.  
  12. def stage_1(cond):
  13. """perform first stage of work,
  14. then notify stage_2 to continue
  15. """
  16. name = multiprocessing.current_process().name
  17. print 'Starting', name
  18. with cond:
  19. print '%s done and ready for stage 2' % name
  20. cond.notify_all()
  21.  
  22. def stage_2(cond):
  23. """wait for the condition telling us stage_1 is done"""
  24. name = multiprocessing.current_process().name
  25. print 'Starting', name
  26. with cond:
  27. cond.wait()
  28. print '%s running' % name
  29.  
  30. if __name__ == '__main__':
  31. condition = multiprocessing.Condition()
  32. s1 = multiprocessing.Process(name='s1',
  33. target=stage_1,
  34. args=(condition,))
  35. s2_clients = [
  36. multiprocessing.Process(name='stage_2[%d]' % i,
  37. target=stage_2,
  38. args=(condition,))
  39. for i in range(1, 3)
  40. ]
  41.  
  42. for c in s2_clients:
  43. c.start()
  44. time.sleep(1)
  45. s1.start()
  46.  
  47. s1.join()
  48. for c in s2_clients:
  49. c.join()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement