【python】多進程鎖multiprocess.Lock

【python】多進程鎖multiprocess.Lock

 分類:

同步的方法基本與多線程相同。python

 

1) Lock多線程

當多個進程須要訪問共享資源的時候,Lock能夠用來避免訪問的衝突。post

[python]  view plain  copy
 
  1. import multiprocessing  
  2. import sys  
  3.   
  4. def worker_with(lock, f):  
  5.     with lock:  
  6.         fs = open(f,"a+")  
  7.         fs.write('Lock acquired via with\n')  
  8.         fs.close()  
  9.           
  10. def worker_no_with(lock, f):  
  11.     lock.acquire()  
  12.     try:  
  13.         fs = open(f,"a+")  
  14.         fs.write('Lock acquired directly\n')  
  15.         fs.close()  
  16.     finally:  
  17.         lock.release()  
  18.   
  19. if __name__ == "__main__":  
  20.   
  21.     f = "file.txt"  
  22.     
  23.     lock = multiprocessing.Lock()  
  24.     w = multiprocessing.Process(target=worker_with, args=(lock, f))  
  25.     nw = multiprocessing.Process(target=worker_no_with, args=(lock, f))  
  26.   
  27.     w.start()  
  28.     nw.start()  
  29.   
  30.     w.join()  
  31.     nw.join()  


     

 
在上面的例子中,若是兩個進程沒有使用lock來同步,則他們對同一個文件的寫操做可能會出現混亂。

 

2)Semaphoreui

Semaphore用來控制對共享資源的訪問數量,例如池的最大鏈接數。spa

[python]  view plain  copy
 
  1. import multiprocessing  
  2. import time   
  3.   
  4. def worker(s,i):  
  5.     s.acquire()  
  6.     print(multiprocessing.current_process().name + " acquire")  
  7.     time.sleep(i)  
  8.     print(multiprocessing.current_process().name + " release")  
  9.     s.release()  
  10.   
  11. if __name__ == "__main__":  
  12.     
  13.     s = multiprocessing.Semaphore(2)  
  14.     for i in range(5):  
  15.         p = multiprocessing.Process(target=worker, args=(s,i*2))  
  16.         p.start()  

 

上面的實例中使用semaphore限制了最多有2個進程同時執行。.net

 

3)Event線程

Event用來實現進程間同步通訊。blog

[python]  view plain  copy
 
  1. import multiprocessing  
  2. import time  
  3.   
  4. def wait_for_event(e):  
  5.     """Wait for the event to be set before doing anything"""  
  6.     print ('wait_for_event: starting')  
  7.     e.wait()  
  8.     print ('wait_for_event: e.is_set()->' + str(e.is_set()))  
  9.   
  10. def wait_for_event_timeout(e, t):  
  11.     """Wait t seconds and then timeout"""  
  12.     print ('wait_for_event_timeout: starting')  
  13.     e.wait(t)  
  14.     print ('wait_for_event_timeout: e.is_set()->' + str(e.is_set()))  
  15.   
  16.   
  17. if __name__ == '__main__':  
  18.     e = multiprocessing.Event()  
  19.     w1 = multiprocessing.Process(name='block',   
  20.                                  target=wait_for_event,  
  21.                                  args=(e,))  
  22.     w1.start()  
  23.   
  24.     w2 = multiprocessing.Process(name='non-block',   
  25.                                  target=wait_for_event_timeout,   
  26.                                  args=(e, 2))  
  27.     w2.start()  
  28.   
  29.     time.sleep(3)  
  30.     e.set()  
  31.     print ('main: event is set')  

#the output is:
#wait_for_event_timeout: starting
#wait_for_event: starting
#wait_for_event_timeout: e.is_set()->False
#main: event is set
#wait_for_event: e.is_set()->True
 
 
相關文章
相關標籤/搜索