python中的Lock
#Lock.py
from multiprocessing import Process,Lock
import os def f(l,i):
l.acquire()
print('hello world %d and Ospid is %s...' %( i,os.getpid()))
l.release()
if __name__=='__main__':
lock = Lock()
for num in range(10):
Process(target=f,args=(lock,num)).start()
有Lock。
结果
hello world 3 and Ospid is 4852...
hello world 2 and Ospid is 540...
hello world 0 and Ospid is 4160...
hello world 1 and Ospid is 948...
hello world 4 and Ospid is 5272...
hello world 6 and Ospid is 3100...
hello world 7 and Ospid is 3364...
hello world 8 and Ospid is 924...
hello world 9 and Ospid is 5196...
hello world 5 and Ospid is 5460...
以下是无Lock
#copy.py
__author__ = 'liunnis'
from multiprocessing import Process,Lock
import os def f(l,i): print('hello world %d and Ospid is %s...' %( i,os.getpid())) if __name__=='__main__':
lock = Lock()
for num in range(10):
p = Process(target=f,args=(lock,num))
p.start()
# p.join()
结果:
hello world 3 and Ospid is 944...
hello world 1 and Ospid is 4156...
hello world 8 and Ospid is 2136...
hello world 5 and Ospid is 3764...
hello world 6 and Ospid is 4876...
hello world 7 and Ospid is 4976...
hello world 2 and Ospid is 2640...
hello world 0 and Ospid is 1404...
hello world 9 and Ospid is 5748...
hello world 4 and Ospid is 4032..
python中的Lock的更多相关文章
- Python 中的Lock与RLock
摘要 由于多线程共享进程的资源和地址空间,因此,在对这些公共资源进行操作时,为了防止这些公共资源出现异常的结果,必须考虑线程的同步和互斥问题. 为什么加锁:1.用于非线程安全, 2.控制一段代码,确保 ...
- Python中的多线程编程,线程安全与锁(一)
1. 多线程编程与线程安全相关重要概念 在我的上篇博文 聊聊Python中的GIL 中,我们熟悉了几个特别重要的概念:GIL,线程,进程, 线程安全,原子操作. 以下是简单回顾,详细介绍请直接看聊聊P ...
- Python进阶(3)_进程与线程中的lock(线程中互斥锁、递归锁、信号量、Event对象、队列queue)
1.同步锁 (Lock) 当全局资源(counter)被抢占的情况,问题产生的原因就是没有控制多个线程对同一资源的访问,对数据造成破坏,使得线程运行的结果不可预期.这种现象称为“线程不安全”.在开发过 ...
- python中协程
在引出协成概念之前先说说python的进程和线程. 进程: 进程是正在执行程序实例.执行程序的过程中,内核会讲程序代码载入虚拟内存,为程序变量分配空间,建立 bookkeeping 数据结构,来记录与 ...
- 线程安全及Python中的GIL
线程安全及Python中的GIL 本博客所有内容采用 Creative Commons Licenses 许可使用. 引用本内容时,请保留 朱涛, 出处 ,并且 非商业 . 点击 订阅 来订阅本博客. ...
- Python中的threading
Python中的threading RLock--重入锁 RLock在Python中的实现是对Lock的封装,具体在类中维护了一个重入次数的变量.一旦一个线程获得一个RLock,该线程再次要求获得该锁 ...
- python中的进程、线程(threading、multiprocessing、Queue、subprocess)
Python中的进程与线程 学习知识,我们不但要知其然,还是知其所以然.你做到了你就比别人NB. 我们先了解一下什么是进程和线程. 进程与线程的历史 我们都知道计算机是由硬件和软件组成的.硬件中的CP ...
- Python 中的进程、线程、协程、同步、异步、回调
进程和线程究竟是什么东西?传统网络服务模型是如何工作的?协程和线程的关系和区别有哪些?IO过程在什么时间发生? 一.上下文切换技术 简述 在进一步之前,让我们先回顾一下各种上下文切换技术. 不过首先说 ...
- python中的多线程【转】
转载自: http://c4fun.cn/blog/2014/05/06/python-threading/ python中关于多线程的操作可以使用thread和threading模块来实现,其中th ...
随机推荐
- [linux] shell脚本编程-xunsearch安装脚本学习
安装脚本setup.sh #!/bin/sh # FULL fast install/upgrade script # See help message via `--help' # $Id$ # s ...
- 单源最短路(Dijkstra算法)
#返回上一级 @Author: 张海拔 @Update: 2015-03-11 @Link: http://www.cnblogs.com/zhanghaiba/p/3514570.html Dijk ...
- 判断php变量是否定义,是否为空,是否为真的一览表
分类: 使用 PHP 函数对变量 $x 进行比较 表达式 gettype() empty() is_null() isset() boolean : if($x) $x = ""; ...
- EMC光纤交换机故障处理和命令分析
主机没有Login到存储是一个比较常见的故障,故障多发于主机新上线,或者是重启后.例如在Unisphere中,显示Host状态是”Registered: Yes; Logged In: No” ...
- 用手机管理及维护MySQL,Oracle等数据库
现在移动办公的情况及需求越来越多,平时MySQL,Oracle,SQLServer等数据库的管理都要通过客户端工具操作,现在有一款基于web网页的软件:TreeSoft数据库管理系统,在服务器布署一套 ...
- 创建Web项目运行时出小错误及解决方法
1.目录结构 2.各文件内容 index.jsp <%@ page contentType="text/html;charset=UTF-8" language=" ...
- Django之ModelForm使用
一:什么是ModelForm呢? Model + Form ==> ModelForm.model和form的结合体,所以有以下功能: 数据验证 数据库操作 model有操作数据库的字段,for ...
- laravel开发之-安装laravel-admin
1.输入命令:composer require encore/laravel-admin "1.4.*" 2.在config/app.php中添加 :Encore\Admin\Pr ...
- JS 和 a href className JS编写顺序与运行顺序 字符串或变量嵌入中括号 代替 点号 代表属性
JS 和 a href 在href里面注意分号结尾, 引号闭合 <a href="javascript:;">空链接</a> <a href=&quo ...
- express中间件笔记整理
expressexpress概念:express是基于nodejs的HTTPS模块构建出来的一个web应用开发框架,在nodejs之上扩展了 Web 应用所需的基本功能.本质上express应用就是调 ...