【python】多进程锁multiprocess.Lock

同步的方法基本与多线程相同。
1) Lock
当多个进程需要访问共享资源的时候,Lock可以用来避免访问的冲突。
- import multiprocessing
- import sys
- def worker_with(lock, f):
- with lock:
- fs = open(f,"a+")
- fs.write('Lock acquired via with\n')
- fs.close()
- def worker_no_with(lock, f):
- lock.acquire()
- try:
- fs = open(f,"a+")
- fs.write('Lock acquired directly\n')
- fs.close()
- finally:
- lock.release()
- if __name__ == "__main__":
- f = "file.txt"
- lock = multiprocessing.Lock()
- w = multiprocessing.Process(target=worker_with, args=(lock, f))
- nw = multiprocessing.Process(target=worker_no_with, args=(lock, f))
- w.start()
- nw.start()
- w.join()
- nw.join()
2)Semaphore
Semaphore用来控制对共享资源的访问数量,例如池的最大连接数。
- import multiprocessing
- import time
- def worker(s,i):
- s.acquire()
- print(multiprocessing.current_process().name + " acquire")
- time.sleep(i)
- print(multiprocessing.current_process().name + " release")
- s.release()
- if __name__ == "__main__":
- s = multiprocessing.Semaphore(2)
- for i in range(5):
- p = multiprocessing.Process(target=worker, args=(s,i*2))
- p.start()
上面的实例中使用semaphore限制了最多有2个进程同时执行。
3)Event
Event用来实现进程间同步通信。
- 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()->' + str(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()->' + str(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()
- time.sleep(3)
- e.set()
- 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】多进程锁multiprocess.Lock的更多相关文章
- 多进程操作-进程锁multiprocess.Lock的使用
多进程操作-进程锁multiprocess.Lock的使用 通过之前的Process模块的学习,我们实现了并发编程,虽然更加充分地利用了IO资源,但是也有缺陷:当多个进程共用一份数据资源的时候,就 ...
- python 多进程锁Lock和共享内存
多进程锁 lock = multiprocessing.Lock() 创建一个锁 lock.acquire() 获取锁 lock.release() 释放锁 with lock: 自动获取.释放锁 类 ...
- 进程同步(multiprocess.Lock、multiprocess.Semaphore、multiprocess.Event) day38
进程同步(multiprocess.Lock.multiprocess.Semaphore.multiprocess.Event) 锁 —— multiprocess.Lock 通过刚刚的学习,我们千 ...
- 铁乐学python_Day39_多进程和multiprocess模块2
铁乐学python_Day39_多进程和multiprocess模块2 锁 -- multiprocess.Lock (进程同步) 之前我们千方百计实现了程序的异步,让多个任务可以同时在几个进程中并发 ...
- Python多进程编程-进程间协作(Queue、Lock、Semaphore、Event、Pipe)
进程与进程之间是相互独立的,互不干扰.如果多进程之间需要对同一资源操作,就需要进程间共享变量,上一篇文章介绍了进程间共享数据的三大类Value.Array.Manager,这三种类的主要区别在于管理的 ...
- Python 之并发编程之进程中(守护进程(daemon)、锁(Lock)、Semaphore(信号量))
五:守护进程 正常情况下,主进程默认等待子进程调用结束之后再结束守护进程在主进程所有代码执行完毕之后,自动终止kill -9 进程号 杀死进程.守护进程的语法:进程对象.daemon = True设置 ...
- python多进程与多线程编程
进程(process)和线程(thread)是非常抽象的概念.多线程与多进程编程对于代码的并发执行,提升代码运行效率和缩短运行时间至关重要.下面介绍一下python的multiprocess和thre ...
- Python多进程与多线程编程及GIL详解
介绍如何使用python的multiprocess和threading模块进行多线程和多进程编程. Python的多进程编程与multiprocess模块 python的多进程编程主要依靠multip ...
- python 多进程开发与多线程开发
转自: http://tchuairen.blog.51cto.com/3848118/1720965 博文作者参考的博文: 博文1 博文2 我们先来了解什么是进程? 程序并不能单独运行,只有将程 ...
随机推荐
- Javascript闭包(狗血剧情,通俗易懂)
我们先来看一个闭包的函数: function a() { var i = 0; function b() { alert(++i); } return b; } var c = a(); c(); c ...
- The Pragmatic Programmer Quick Reference Guide
1.关心你的技艺 Care About Your Craft 如果不在乎能否漂亮地开发出软件,你又为何要耗费生命去开发软件呢? 2.思考!你的工作 Think! About Your Work 关掉自 ...
- 每日总结 -----把人家代码干掉了 我恨git
今天搞了下午git,写完代码commit之后,pull完发现没法push,说是和origin有分支,然后自己查资料又是reset又是rebase的,commit之后发现自己改动的代码几乎没有被提交上去 ...
- 用ul 来制作导航栏的几个要点
1 ul 1)list-style:none; 3)设置宽度 2)清除浮动的影响,使高度自适应 2 li 1)向左浮动 2)设置margin和padding都为0 3 a 1) dsiplay :bl ...
- 内存对齐 和 sizeof小结
数据对齐(内存对齐)指该数据所在的地址必须是该数据长度的整数倍.X86CPU能直接访问对齐的数据,当它试图访问未对齐的数据时,会在内部进行一系列的调整,降低运行速度.数据对齐一般出现在结构体和类中,在 ...
- Liunx的各种小指令
tsshutdown -y ----关闭所有服务tsboot -g COMMON ----启动公共服务tsboot -g ETXX ----启动XX服务 tsma -e oet1 -l 172.X ...
- SRS文档
1什么是用例? 在介始用例方法之前,我们首先来看一下传统的需求表述方式-"软件需求规约"(Software Requirement Specification).传统的软件需求规约 ...
- MMU内存管理单元相关知识点总结
1.MMU是Memory Management Unit的缩写,中文名是内存管理单元,它是中央处理器(CPU)中用来管理虚拟存储器.物理存储器的控制线路,同时也负责虚拟地址映射为物理地址,以及提供硬件 ...
- C++ Daily 《5》----虚函数表的共享问题
问题: 包含一个以上虚函数的 class B, 它所定义的 对象是否共用一个虚函数表? 分析: 由于含有虚函数,因此对象内存包含了一个指向虚函数表的指针,但是这个指针指向的是同一个虚函数表吗? 实验如 ...
- linux git 推送空文件夹
/********************************************************************************* * linux git 推送空文件 ...