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 ...
随机推荐
- WordPress主题开发:主题初始化
在最简单的情况下,一个WordPress主题由两个文件构成: index.php ------------------主模版 style.css -------------------主样式表(注意 ...
- 再说优化MySQL索引
这几天开发尤其重视数据库索引的优化,是一件好事情,开发特意提过来几个要删除的索引,oh!我的佛陀呀!历史上出现过因为评估不到位,删索引引发故障的案例.那么有什么办法可以评估索引是否合理呢? perco ...
- C# 中的隐式类型转换(运算时的隐式转换)和显示类型转换
区别: 隐式转换失败编译会报错. 显示转换有可能精度丢失. 根据项目的编译设置,显示转换溢出可能会报错,如果设置溢出且未使用checked检查,运行时如果发生溢出会产出未知的计算结果. 在数字运算时, ...
- 在Ubuntu16.04.4和Windows10上安装jdk
在Ubuntu16.04.4和Windows10上安装jdk 一.在Ubuntu16.04.4上安装jdk 1.下载jdk安装包 首先我们在oracle官网上下载jdk-8u161-linu ...
- nodejs简易代理服务器
直接代码: var http = require('http') var proxy = http.createServer(function (request, response) { var op ...
- Best MVC Practices 最佳的MVC实践
Although Model-View-Controller (MVC) is known by nearly every Web developer, how to properly use MVC ...
- [PHP] apache在worker模式配置fastcgi使用php-fpm
1.准备: dpkg -L apache2查看所有安装的apache2的应用 a2query -M查看apache2使用的模式 httpd -l旧版本查看当前apache模式 2.查看apache的进 ...
- 网站压力测试 工具webbench
webbench最多可以模拟3万个并发连接去测试网站的负载能力,个人感觉要比Apache自带的ab压力测试工具好用,安装使用也特别方便,并且非常小. 主要是 -t 参数用着比较爽,下面参考了张宴的文章 ...
- C#实现局部峰值查找,功能对应Matlab中的findpeaks.m
相关算法的原理参考Ronny,地址:图像分析:投影曲线的波峰查找,这里感谢下原作者. 参照C++的代码实现,我用C#翻译了下,其实原理也很简单的,下面放相关实现代码: private double[] ...
- Tarjan系列1
tajan的dfs树系列算法: 求解割点,桥,强连通分量,点双联通分量,边双联通分量: tajan是一个dfs,把一个图变成一个dfs树结构, dfs树结构,本质是通过一个没有任何要求的dfs把图的边 ...