python 多进程中锁的使用方法
生活随笔
收集整理的這篇文章主要介紹了
python 多进程中锁的使用方法
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一、不加鎖
from multiprocessing import Process,Lock import timedef get():for i in range(3):time.sleep(1)print(i)def task(lock):# lock.acquire() #獲取鎖get()# lock.release() #釋放鎖if __name__ == '__main__':lock=Lock()for i in range(5): #并發5個p=Process(target=task,args=(lock,))p.start()二、加鎖
from multiprocessing import Process,Lock import timedef get():for i in range(3):time.sleep(1)print(i)def task(lock):lock.acquire() #獲取鎖get()lock.release() #釋放鎖if __name__ == '__main__':lock=Lock()for i in range(5): #并發5個p=Process(target=task,args=(lock,))p.start()總結
以上是生活随笔為你收集整理的python 多进程中锁的使用方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: kafka消费中的partition与消
- 下一篇: python3中的dict循环性能对比