zoukankan      html  css  js  c++  java
  • 【python】多进程锁multiprocess.Lock

    【python】多进程锁multiprocess.Lock

     分类:

    同步的方法基本与多线程相同。

    1) Lock

    当多个进程需要访问共享资源的时候,Lock可以用来避免访问的冲突。

    [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 ')  
    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 ')  
    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)Semaphore

    Semaphore用来控制对共享资源的访问数量,例如池的最大连接数。

    [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个进程同时执行。

    3)Event

    Event用来实现进程间同步通信。

    [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
     
     
  • 相关阅读:
    Python多态
    python 元类与定制元类
    Python:类属性,实例属性,私有属性与静态方法,类方法,实例方法
    Python 类 --基础与要点
    服务器错误401
    H5中使用Web Storage来存储结构化数据
    Web Storage
    定义表单控件的id和name注意点
    Annotation(注解)代替配置文件
    jQuery 特殊选择器this
  • 原文地址:https://www.cnblogs.com/shijingjing07/p/6204421.html
Copyright © 2011-2022 走看看