Python 中的Lock与RLock
摘要
由于多线程共享进程的资源和地址空间,因此,在对这些公共资源进行操作时,为了防止这些公共资源出现异常的结果,必须考虑线程的同步和互斥问题。
为什么加锁:1、用于非线程安全, 2、控制一段代码,确保其不产生调度混乱。
threading.Lock的用法
下面是一个python多线程的例子:
import threading
count = 0
def print_time(threadName):
global count
c=0
while(c<100):
c+=1
count+=1
print("{0}: set count to {1}".format( threadName, count) )
try:
threading.Thread( target=print_time, args=("Thread-1", ) ).start()
threading.Thread( target=print_time, args=("Thread-2", ) ).start()
threading.Thread( target=print_time, args=("Thread-3", ) ).start()
except Exception as e:
print("Error: unable to start thread")
在这个例子中,我们start了3个线程,每个线程都会对全局资源count
进行改写操作。得到的结果如下,每个thread都会交替对count值进行修改。
Thread-1: set count to 198
Thread-2: set count to 199
Thread-1: set count to 200
Thread-2: set count to 201
Thread-1: set count to 202
Thread-2: set count to 203
Thread-1: set count to 204
Thread-2: set count to 205
我们可以对上例中的print_time()中访问资源的代码加锁,就可以把这个函数变为线程安全的函数。具体代码如下:
import threading
count = 0
lock = threading.Lock()
def print_time(threadName):
global count
c=0
with lock:
while(c<100):
c+=1
count+=1
print("{0}: set count to {1}".format( threadName, count) )
try:
threading.Thread( target=print_time, args=("Thread-1", ) ).start()
threading.Thread( target=print_time, args=("Thread-2", ) ).start()
threading.Thread( target=print_time, args=("Thread-3", ) ).start()
except Exception as e:
print("Error: unable to start thread")
通过threading.Lock(),就能实现加锁。这样每个thread对count进行改动期间while(c<100)
,就不会有其它的thread插入进来改动count。得到的输出如下:
Thread-2: set count to 199
Thread-2: set count to 200
Thread-3: set count to 201
Thread-3: set count to 202
Thread-3: set count to 203
Thread-3: set count to 204
锁的使用,有两种方法,上面的是最简单的通过with lock
来操作。
还有另一种用法,是通过Lock的acquire()和release()函数
来控制加锁和解锁,如下例,得到的结果和上例相同:
import threading
count = 0
lock = threading.Lock()
def print_time(threadName):
global count
c=0
if(lock.acquire()):
while(c<100):
c+=1
count+=1
print("{0}: set count to {1}".format( threadName, count) )
lock.release()
try:
threading.Thread( target=print_time, args=("Thread-1", ) ).start()
threading.Thread( target=print_time, args=("Thread-2", ) ).start()
threading.Thread( target=print_time, args=("Thread-3", ) ).start()
except Exception as e:
print("Error: unable to start thread")
Lock与RLock的区别
在threading模块中,定义两种类型的琐:threading.Lock和threading.RLock。它们之间有一点细微的区别,通过比较下面两段代码来说明:
import threading
lock = threading.Lock() #Lock对象
lock.acquire()
lock.acquire() #产生了死琐。
lock.release()
lock.release()
import threading
rLock = threading.RLock() #RLock对象
rLock.acquire()
rLock.acquire() #在同一线程内,程序不会堵塞。
rLock.release()
rLock.release()
这两种琐的主要区别是:RLock允许在同一线程中被多次acquire。而Lock却不允许这种情况
。注意:如果使用RLock
,那么acquire和release必须成对出现
,即调用了n次acquire,必须调用n次的release才能真正释放所占用的琐。
Python 中的Lock与RLock的更多相关文章
- python threading编程中的LOCK和RLOCK(可重入锁)
找到一本PYTHON并发编辑的书, 弄弄.. #!/usr/bin/env python # -*- coding: utf-8 -*- import threading import time sh ...
- python中的Lock
#Lock.py from multiprocessing import Process,Lock import os def f(l,i): l.acquire() print('hello wor ...
- Python中的多线程编程,线程安全与锁(一)
1. 多线程编程与线程安全相关重要概念 在我的上篇博文 聊聊Python中的GIL 中,我们熟悉了几个特别重要的概念:GIL,线程,进程, 线程安全,原子操作. 以下是简单回顾,详细介绍请直接看聊聊P ...
- Python进阶(3)_进程与线程中的lock(线程中互斥锁、递归锁、信号量、Event对象、队列queue)
1.同步锁 (Lock) 当全局资源(counter)被抢占的情况,问题产生的原因就是没有控制多个线程对同一资源的访问,对数据造成破坏,使得线程运行的结果不可预期.这种现象称为“线程不安全”.在开发过 ...
- python threading模块的Lock和RLock区别
首先了解这两者是什么. 以下说明参考自python官网 Lock:Lock被称为①原始锁,原始锁是一个②在锁定时不属于特定线程的同步基元组件,它是能用的最低级的同步基元组件.原始锁处于 "锁 ...
- python多线程锁lock/Rlock/BoundedSemaphore/Condition/Event
import time import threading lock = threading.RLock() n = 10 def task(arg): # 加锁,此区域的代码同一时刻只能有一个线程执行 ...
- Python中的threading
Python中的threading RLock--重入锁 RLock在Python中的实现是对Lock的封装,具体在类中维护了一个重入次数的变量.一旦一个线程获得一个RLock,该线程再次要求获得该锁 ...
- python中的进程、线程(threading、multiprocessing、Queue、subprocess)
Python中的进程与线程 学习知识,我们不但要知其然,还是知其所以然.你做到了你就比别人NB. 我们先了解一下什么是进程和线程. 进程与线程的历史 我们都知道计算机是由硬件和软件组成的.硬件中的CP ...
- python中的多线程【转】
转载自: http://c4fun.cn/blog/2014/05/06/python-threading/ python中关于多线程的操作可以使用thread和threading模块来实现,其中th ...
随机推荐
- 【canvas学习笔记三】样式和颜色
上一节我们学习了如何用路径绘制各种形状,但我们只能用默认的颜色和线条.这节就来学习设置不同的颜色和线条样式. 颜色 设置颜色主要有两个属性: fillStyle = color 设置填充颜色 stro ...
- Python3学习笔记(九):赋值,浅拷贝和深拷贝区别
一.变量赋值 在Python可变数据类型(列表,字典,集合)中,把一个可变数据类型的变量赋给另一个变量,这两个变量引用的是同一个对象,内存地址是一样的,修改当中的一个变量,另一个变量相应也会被修改 & ...
- Centos7源码安装jdk
1. 下载jdk : jdk-8u211-linux-x64.tar.gz https://www.oracle.com/technetwork/java/javase/downloads/jdk8 ...
- Tomcat部署时war和war exploded区别以及平时踩得坑
war和war exploded的区别 在使用IDEA开发项目的时候,部署Tomcat的时候通常会出现下边的情况: 是选择war还是war exploded 这里首先看一下他们两个的区别: war模式 ...
- SpringBoot,用200行代码完成一个一二级分布式缓存
缓存系统的用来代替直接访问数据库,用来提升系统性能,减小数据库复杂.早期缓存跟系统在一个虚拟机里,这样内存访问,速度最快. 后来应用系统水平扩展,缓存作为一个独立系统存在,如redis,但是每次从缓存 ...
- SecureCRT使用+堡垒机简单使用
写在前面的话 自从升级为宝妈后,回来发现好多东西都遗忘了.熟话说:好记性不如烂笔头,我还是记录下来吧. 堡垒机使用的几个技巧 1.快捷操作 1) 输入 ID 直接登录. ...
- 一、基础篇--1.1Java基础-包装类的装箱和拆箱
包装类:java是典型的面向对象编程,但是八种基本数据类型并不支持面向对象编程.基本类型的数据不具备对象的特性,没有属性和方法.沿用它们只是为了迎合人类根深蒂固的习惯,并的确能简单.有效地进行常规数据 ...
- Python 使用Qt进行开发(二)
上次简单实现了显示窗口,下面我们在窗口中加入一些部件. 1,我们在窗口中使用 setToolTip() 方法添加一个文本提示,在窗口中鼠标暂停几秒即可显示该文本信息. class test(): de ...
- 一、Jmeter启动报错:Could not initialize class org.apache.jmeter.gui.util.MenuFactory
1.下载: plugins-manager.jar 包 2.地址:https://jmeter-plugins.org/install/Install/ 3.将jar包放到lib/ext 4.重启jm ...
- 红帽虚拟化RHEV3.2创建虚拟机(图文Step by Step)
目录 目录 前言 Install RHEV 创建Data CenterClusterHost 创建存储 创建虚拟机 前言 RHEV3.2的Web管理界面有了很大的改进,更加的简单和便捷,还可以使用中文 ...