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 ...
随机推荐
- JS实现最短路径之迪杰斯特拉(Dijkstra)算法
最短路径: 对于网图来说,最短路径是指两个顶点之间经过的边上权值和最少的路径,我们称第一个顶点是源点,最后一个顶点是终点 迪杰斯特拉 ( Dijkstra) 算法是并不是一下子就求出 了 Vo 到V8 ...
- 你所需要的sql数据库资料
sql语法的特点 1.没有"",所有的字符串都使用''包含 2.它的逻辑相等与赋值运算符一样都是= 如 if 1=1 3.不区别大小写,但是习惯函数上使用大写.所有与数据库 ...
- log4php使用及配置
log4php使用及配置 1.在项目中加入log4php包 2.log4php配置 在项目配置包中添加logger_config.xml配置文件: logger_config.xml配置文件添加代码如 ...
- BZOJ4671:异或图
传送门 直接求连通的不好做,考虑容斥 设 \(g_i\) 表示至少有 \(i\) 个连通块的方案数,\(f_i\) 表示恰好有 \(i\) 个的 那么 \[g_x=\sum_{i=x}^{n}\beg ...
- Bzoj3197: [Sdoi2013]assassin
题面 传送门 Sol 套路:找出重心,如果有两个就新建一个点 然后把这棵树hash一下 设\(f[i][j]\)表示第一颗树到\(i\)第二棵树到\(j\),子树\(i,j\)同构的付出的最小代价 转 ...
- Linux 添加yum命令
第一步,国内的yum仓库 http://mirrors.163.com/centos/7/os/x86_64/Packages/ http://mirrors.aliyun.com/centos/7/ ...
- C++学习笔记(3)----类模板的static成员
与任何其他类相同,类模板可以声明 static 成员: template <typename T> class Foo { public: static std::size_t count ...
- Web Api ——创建WebAPI
方法在Win10 + VS2017(MVC5)测试通过 1.建立 WebApi项目: 选择菜单 “文件->新建醒目->web ->ASP.NET Web 应用程序” 输入项目名称和位 ...
- 使用ajax请求后返回数据显示undefinded解决办法
今天在使用七牛的sdk做断点续传时候,当文件过大,本地没有存储时候,想要通过ajax将本地信息存到服务器缓存,之后通过读取缓存的方式来完成文件过大断点续传无法使用问题. 但是在使用ajax请求的时候, ...
- css3圆形光环闪烁效果
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...