【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----拖拽
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- [SHELL] 修改xml的内容
解析和修改xml用python比较方便,但如果不方便使用python,可以用sed命令简单替换 例如,欲替换下面一行中的端口号的值: <param name="ftpPort" ...
- C语言程序设计第7堂作业
一.本次课主要内容: 本次以计算圆柱体体积为例,通过定义体积计算功能的函数和主函数调用的例子,引出函数定义的一般形式:函数首部加函数体,且在函数结尾处通过return 语句返回结果.本节要重 ...
- Mybatis3.x与Spring4.x整合(转)
http://www.cnblogs.com/xdp-gacl/p/4271627.html 一.搭建开发环境 1.1.使用Maven创建Web项目 执行如下命令: mvn archetype:cre ...
- yii2.0邮箱发送
邮件发送配置: 打开配置文件将下面代码添加到 components => [...]中(例:高级版默认配置在/common/config/main-local.php) 'mai ...
- How can i use iptables save on centos 7?
I installed CentOS 7 with minimal configuration (os + dev tools). I am trying to open 80 port for ht ...
- #import和@class的使用
#import #import 大部分功能和#include是一样的,但是他处理了重复引用的问题,不用再去自己进行重复引用处理. @class 用于声明一个类,告诉编 ...
- 【海洋女神原创】How to: Installshield做安装包时如何添加文件
我一直以为这不是一个问题,可是没想到在几个群内,对于如何向安装包添加文件不解的大有人在,今日稍暇,整理成篇,以供参考 首先我想再大声地说一遍:不要再跟我说英文看不懂了!!!!你做了程序员这一行,就得逼 ...
- 基于CSS的幻灯片工具 reveal.js
官网:http://lab.hakim.se/reveal-js/#/ github https://github.com/hakimel/reveal.js 更多资源:6个最好的 HTML5/CS ...
- 怎么学JavaScript?
作者:小不了链接:https://zhuanlan.zhihu.com/p/23265155来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 鉴于时不时,有同学私信问我( ...