[code.view]

[top] / python / PyMOTW / multiprocessing / multiprocessing_event.py

     #!/usr/bin/env python
     # encoding: utf-8
     #
     # Copyright (c) 2008 Doug Hellmann All rights reserved.
     #
     """Using Events to synchronize threads.
     """
     #end_pymotw_header
     
     import multiprocessing
     import time
     
     def wait_for_event(e):
         """Wait for the event to be set before doing anything"""
         print 'wait_for_event: starting'
         e.wait()
         print 'wait_for_event: e.is_set()->', e.is_set()
     
     def wait_for_event_timeout(e, t):
         """Wait t seconds and then timeout"""
         print 'wait_for_event_timeout: starting'
         e.wait(t)
         print 'wait_for_event_timeout: e.is_set()->', e.is_set()
     
     
     if __name__ == '__main__':
         e = multiprocessing.Event()
         w1 = multiprocessing.Process(name='block', 
                                      target=wait_for_event,
                                      args=(e,))
         w1.start()
     
         w2 = multiprocessing.Process(name='non-block', 
                                      target=wait_for_event_timeout, 
                                      args=(e, 2))
         w2.start()
     
         print 'main: waiting before calling Event.set()'
         time.sleep(3)
         e.set()
         print 'main: event is set'
     

[top] / python / PyMOTW / multiprocessing / multiprocessing_event.py

contact | logmethods.com