python类库32[多进程同步Lock+Semaphore+Event]
python类库32[多进程同步Lock+Semaphore+Event]
同步的方法基本与多线程相同。
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
参考:http://www.doughellmann.com/PyMOTW/multiprocessing/communication.html
完!
python类库32[多进程同步Lock+Semaphore+Event]的更多相关文章
- python类库32[多进程通信Queue+Pipe+Value+Array]
多进程通信 queue和pipe的区别: pipe用来在两个进程间通信.queue用来在多个进程间实现通信. 此两种方法为所有系统多进程通信的基本方法,几乎所有的语言都支持此两种方法. 1)Queue ...
- python lock, semaphore, event实现线程同步
lock 机制不管你是java, C#, 还是python都是常用的线程同步机制, 相比较C# 的锁机制, python的加锁显得比较简单, 直接调用threading 标准库的lock 就可以了. ...
- python类库32[序列化和反序列化之pickle]
一 pickle pickle模块用来实现python对象的序列化和反序列化.通常地pickle将python对象序列化为二进制流或文件. python对象与文件之间的序列化和反序列化: pi ...
- [b0041] python 归纳 (二六)_多进程数据共享和同步_事件Event
# -*- coding: utf-8 -*- """ 多进程 同步 事件multiprocessing.Event 逻辑: 子线程负责打印,会阻塞, 等待主进程发出控制 ...
- 扯扯python的多线程的同步锁 Lock RLock Semaphore Event Condition
我想大家都知道python的gil限制,记得刚玩python那会,知道了有pypy和Cpython这样的解释器,当时听说是很猛,也就意味肯定是突破了gil的限制,最后经过多方面测试才知道,还是那德行… ...
- python 多线程中的同步锁 Lock Rlock Semaphore Event Conditio
摘要:在使用多线程的应用下,如何保证线程安全,以及线程之间的同步,或者访问共享变量等问题是十分棘手的问题,也是使用多线程下面临的问题,如果处理不好,会带来较严重的后果,使用python多线程中提供Lo ...
- Python系列之 - 锁(GIL,Lock,Rlock,Event,信号量)
python 的解释器,有很多种,但市场占有率99.9%的都是基于c语言编写的CPython. 在这个解释器里规定了GIL. In CPython, the global interpreter l ...
- Python 多线程、多进程 (二)之 多线程、同步、通信
Python 多线程.多进程 (一)之 源码执行流程.GIL Python 多线程.多进程 (二)之 多线程.同步.通信 Python 多线程.多进程 (三)之 线程进程对比.多线程 一.python ...
- python多线程同步机制Semaphore
#!/usr/bin/env python # -*- coding: utf-8 -*- """ Python 线程同步机制:Semaphore "" ...
随机推荐
- 63不同路径II
题目: 一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为“Start” ).机器人每次只能向下或者向右移动一步.机器人试图达到网格的右下角(在下图中标记为“Finish”).现在考 ...
- django 通过模型类操作数据表(基础增删改查)
进入项目shell的命令: python manage.py shell 以下为在相互shell终端中演示的例子: 首先导入模型类:(from 应用名.models import 类名) from b ...
- playbook部署nginx
定义playbook的主机组 说明: 1.playbook的主机组和ansible的主机组不一样, 2.playbook的主机组文件必须要和playbook文件在同一个目录下否则会报如下错误: [ro ...
- Gin框架中文文档
Gin 是一个 go 写的 web 框架,具有高性能的优点.官方地址:https://github.com/gin-gonic/gin 带目录请移步 http://xf.shuangdeyu.com/ ...
- ${pagecontext.request.contextpath}绝对路径理解
${pageContext.request.contextPath}是JSP取得绝对路径的方法,等价于<%=request.getContextPath()%> .也就是取出部署的应用程序 ...
- 小记------查看‘阿里云机器’yarn 日志
通过ip:8088 页面 复制正在运行的application ID 在linux客户端执行 xshell yarn logs -applicationId application_155869 ...
- CentOS添加使用
在本机安装虚拟机,虚拟机安装CentSO.也可以装双系统,双系统问题更多 环境:win7 64 位 1.查看电脑是否可虚拟化(在百度查) 2.查看电脑是否打开虚拟机设置,如果没有,百度如何开启 打开虚 ...
- qDebug的用法
qDebug用于在控制台输出调试信息,主要有以下几种用法. 1.类似c++的cout函数 QString str="world"; qDebug()<<"he ...
- git diff 命令用法
理解git diff的前提,首先要理解git中工作区,暂存区,本地版本库的概念,如果头脑中有这些概念,接着往下读. git diff test.c 用来查看工作区和暂存区中test.c文件的区别. g ...
- Python基础学习——文件操作、函数
一.文件操作 文件操作链接:http://www.cnblogs.com/linhaifeng/articles/5984922.html(更多内容见此链接) 一.对文件操作流程 打开文件,得到文件句 ...