Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # encoding: utf-8
- #
- # Copyright (c) 2008 Doug Hellmann All rights reserved.
- #
- """Using a Condition to control sequencing between workers.
- """
- #end_pymotw_header
- import multiprocessing
- import time
- def stage_1(cond):
- """perform first stage of work,
- then notify stage_2 to continue
- """
- name = multiprocessing.current_process().name
- print 'Starting', name
- with cond:
- print '%s done and ready for stage 2' % name
- cond.notify_all()
- def stage_2(cond):
- """wait for the condition telling us stage_1 is done"""
- name = multiprocessing.current_process().name
- print 'Starting', name
- with cond:
- cond.wait()
- print '%s running' % name
- if __name__ == '__main__':
- condition = multiprocessing.Condition()
- s1 = multiprocessing.Process(name='s1',
- target=stage_1,
- args=(condition,))
- s2_clients = [
- multiprocessing.Process(name='stage_2[%d]' % i,
- target=stage_2,
- args=(condition,))
- for i in range(1, 3)
- ]
- for c in s2_clients:
- c.start()
- time.sleep(1)
- s1.start()
- s1.join()
- for c in s2_clients:
- c.join()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement