python之多线程 threading.Lock() 和 threading.RLock()
0.目录
2. threading.Lock() 的必要性
3.观察block
4.threading.RLock() 的应用场景
1.参考
Thread Synchronization Mechanisms in Python
count += 1 不是原子操作,三步操作可能被中断,通过lock将三步操作“封装”为一步操作,要么执行,要么不执行。
counter = 0 def process_item(item):
global counter
... do something with item ...
counter += 1 # The reason for this is that the increment operation is actually executed in three steps;
#first, the interpreter fetches the current value of the counter,
# then it calculates the new value,
# and finally, it writes the new value back to the variable.
Atomic Operations #
The simplest way to synchronize access to shared variables or other resources is to rely on atomic operations in the interpreter.
An atomic operation is an operation that is carried out in a single execution step, without any chance that another thread gets control.
What kinds of global value mutation are thread-safe?
python的原子操作
A global interpreter lock (GIL) is used internally to ensure that only one thread runs in the Python VM at a time. In general, Python offers to switch among threads only between bytecode instructions; how frequently it switches can be set via sys.setcheckinterval(). Each bytecode instruction and therefore all the C implementation code reached from each instruction is therefore atomic from the point of view of a Python program. In theory, this means an exact accounting requires an exact understanding of the PVM bytecode implementation. In practice, it means that operations on shared variables of built-in data types (ints, lists, dicts, etc) that “look atomic” really are. For example, the following operations are all atomic (L, L1, L2 are lists, D, D1, D2 are dicts, x, y are objects, i, j are ints): L.append(x)
L1.extend(L2)
x = L[i]
x = L.pop()
L1[i:j] = L2
L.sort()
x = y
x.field = y
D[x] = y
D1.update(D2)
D.keys() These aren’t: i = i+1
L.append(L[-1])
L[i] = L[j]
D[x] = D[x] + 1 Operations that replace other objects may invoke those other objects’ __del__() method when their reference count reaches zero, and that can affect things. This is especially true for the mass updates to dictionaries and lists. When in doubt, use a mutex!
2. threading.Lock() 的必要性
#!usr/bin/env python
#coding:utf-8
import sys
import time
import random
import logging import threading
import Queue lock = threading.Lock() #'function-call ownership'
rlock = threading.RLock() #thread ownership logging.basicConfig(level=logging.DEBUG,
format = '%(asctime)s - %(threadName)-10s - %(levelname)s - %(message)s')
logger = logging.getLogger() count = 0 class MyThread(threading.Thread):
def __init__(self):
threading.Thread.__init__(self) def run(self):
global count for i in range(100):
count += 1
logger.debug('{} finished, count is {}'.format(self.name, count)) def main():
logger.debug('initial count: {}'.format(count)) thread_list = [MyThread() for i in range(2)]
for t in thread_list:
t.start()
for t in thread_list:
t.join() logger.debug('final count: {}'.format(count)) if __name__ == '__main__':
main()
修改run函数代码的不同输出:
def run(self):
global count for i in range(100):
count += 1
logger.debug('{} finished, count is {}'.format(self.name, count))
# 在切换线程之前,某一线程已经完成,两个线程顺序完成,结果几乎不会有误
# 2017-08-20 12:19:30,857 - MainThread - DEBUG - initial count: 0
# 2017-08-20 12:19:30,858 - Thread-1 - DEBUG - Thread-1 finished, count is 100
# 2017-08-20 12:19:30,858 - Thread-2 - DEBUG - Thread-2 finished, count is 200
# 2017-08-20 12:19:30,858 - MainThread - DEBUG - final count: 200 time.sleep(0.001)
for i in range(100):
count += 1
logger.debug('{} finished, count is {}'.format(self.name, count))
# 开头sleep导致两个线程几乎同时启动,结果可能有误
# 2017-08-20 12:24:59,046 - MainThread - DEBUG - initial count: 0
# 2017-08-20 12:24:59,048 - Thread-2 - DEBUG - Thread-2 finished, count is 124
# 2017-08-20 12:24:59,048 - Thread-1 - DEBUG - Thread-1 finished, count is 153
# 2017-08-20 12:24:59,048 - MainThread - DEBUG - final count: 153 for i in range(10000):
count += 1
logger.debug('{} finished, count is {}'.format(self.name, count))
# bytecodes足够导致两个线程交替运行,结果大概率有误
# 2017-08-20 12:20:17,719 - MainThread - DEBUG - initial count: 0
# 2017-08-20 12:20:17,723 - Thread-1 - DEBUG - Thread-1 finished, count is 12438
# 2017-08-20 12:20:17,723 - Thread-2 - DEBUG - Thread-2 finished, count is 12616
# 2017-08-20 12:20:17,723 - MainThread - DEBUG - final count: 12616 with lock:
for i in range(10000):
count += 1
logger.debug('{} finished, count is {}'.format(self.name, count))
# lock直到某一线程完成,结果正确
# 2017-08-20 12:20:37,630 - MainThread - DEBUG - initial count: 0
# 2017-08-20 12:20:37,631 - Thread-1 - DEBUG - Thread-1 finished, count is 10000
# 2017-08-20 12:20:37,632 - Thread-2 - DEBUG - Thread-2 finished, count is 20000
# 2017-08-20 12:20:37,634 - MainThread - DEBUG - final count: 20000 for i in range(10000):
with lock:
count += 1
logger.debug('{} finished, count is {}'.format(self.name, count))
# 两个线程交替lock,结果正确
# 2017-08-20 12:21:03,921 - MainThread - DEBUG - initial count: 0
# 2017-08-20 12:21:03,973 - Thread-1 - DEBUG - Thread-1 finished, count is 19979
# 2017-08-20 12:21:03,973 - Thread-2 - DEBUG - Thread-2 finished, count is 20000
# 2017-08-20 12:21:03,973 - MainThread - DEBUG - final count: 20000
3.观察block
def run(self):
global count all = range(10000) #确保每个线程 +1 的次数
while all != []:
if not lock.acquire(False): #假设没有参数会导致block,则马上返回false当不block;否则返回true且acquire
logger.debug('{} wait...{}'.format(self.name, len(all)))
else:
try:
count += 1
all.pop()
except Exception as err:
logger.debug('{} err, count is {}'.format(self.name, count))
finally:
# logger.debug('{} release {} {}'.format(self.name, count, len(all))) #导致两个线程顺序执行???
lock.release()
logger.debug('{} finished, count is {}'.format(self.name, count))
输出:
2017-08-20 12:32:55,204 - MainThread - DEBUG - initial count: 0
2017-08-20 12:32:55,210 - Thread-1 - DEBUG - Thread-1 wait...9925
2017-08-20 12:32:55,210 - Thread-1 - DEBUG - Thread-1 wait...9925
2017-08-20 12:32:55,210 - Thread-1 - DEBUG - Thread-1 wait...9925
2017-08-20 12:32:55,210 - Thread-1 - DEBUG - Thread-1 wait...9925
2017-08-20 12:32:55,210 - Thread-1 - DEBUG - Thread-1 wait...9925
2017-08-20 12:32:55,210 - Thread-1 - DEBUG - Thread-1 wait...9925
2017-08-20 12:32:55,210 - Thread-1 - DEBUG - Thread-1 wait...9925
2017-08-20 12:32:55,211 - Thread-1 - DEBUG - Thread-1 wait...9925
2017-08-20 12:32:55,213 - Thread-1 - DEBUG - Thread-1 wait...9925
2017-08-20 12:32:55,213 - Thread-1 - DEBUG - Thread-1 wait...9925
2017-08-20 12:32:55,213 - Thread-1 - DEBUG - Thread-1 wait...9925
2017-08-20 12:32:55,213 - Thread-1 - DEBUG - Thread-1 wait...9925
2017-08-20 12:32:55,214 - Thread-1 - DEBUG - Thread-1 wait...9925
2017-08-20 12:32:55,214 - Thread-1 - DEBUG - Thread-1 wait...9925
2017-08-20 12:32:55,214 - Thread-1 - DEBUG - Thread-1 wait...9925
2017-08-20 12:32:55,214 - Thread-1 - DEBUG - Thread-1 wait...9925
2017-08-20 12:32:55,216 - Thread-2 - DEBUG - Thread-2 wait...6036
2017-08-20 12:32:55,216 - Thread-2 - DEBUG - Thread-2 wait...6036
2017-08-20 12:32:55,216 - Thread-2 - DEBUG - Thread-2 wait...6036
2017-08-20 12:32:55,216 - Thread-2 - DEBUG - Thread-2 wait...6036
2017-08-20 12:32:55,216 - Thread-2 - DEBUG - Thread-2 wait...6036
2017-08-20 12:32:55,217 - Thread-2 - DEBUG - Thread-2 wait...6036
2017-08-20 12:32:55,217 - Thread-2 - DEBUG - Thread-2 wait...6036
2017-08-20 12:32:55,217 - Thread-2 - DEBUG - Thread-2 wait...6036
2017-08-20 12:32:55,217 - Thread-2 - DEBUG - Thread-2 wait...6036
2017-08-20 12:32:55,217 - Thread-2 - DEBUG - Thread-2 wait...6036
2017-08-20 12:32:55,217 - Thread-2 - DEBUG - Thread-2 wait...6036
2017-08-20 12:32:55,217 - Thread-2 - DEBUG - Thread-2 wait...6036
2017-08-20 12:32:55,217 - Thread-2 - DEBUG - Thread-2 wait...6036
2017-08-20 12:32:55,217 - Thread-2 - DEBUG - Thread-2 wait...6036
2017-08-20 12:32:55,219 - Thread-2 - DEBUG - Thread-2 wait...6036
2017-08-20 12:32:55,219 - Thread-2 - DEBUG - Thread-2 wait...6036
2017-08-20 12:32:55,219 - Thread-2 - DEBUG - Thread-2 wait...6036
2017-08-20 12:32:55,219 - Thread-2 - DEBUG - Thread-2 wait...6036
2017-08-20 12:32:55,219 - Thread-2 - DEBUG - Thread-2 wait...6036
2017-08-20 12:32:55,220 - Thread-2 - DEBUG - Thread-2 wait...6036
2017-08-20 12:32:55,220 - Thread-2 - DEBUG - Thread-2 wait...6036
2017-08-20 12:32:55,220 - Thread-2 - DEBUG - Thread-2 wait...6036
2017-08-20 12:32:55,220 - Thread-2 - DEBUG - Thread-2 wait...6036
2017-08-20 12:32:55,220 - Thread-2 - DEBUG - Thread-2 wait...6036
2017-08-20 12:32:55,220 - Thread-2 - DEBUG - Thread-2 wait...6036
2017-08-20 12:32:55,220 - Thread-2 - DEBUG - Thread-2 wait...6036
2017-08-20 12:32:55,220 - Thread-2 - DEBUG - Thread-2 wait...6036
2017-08-20 12:32:55,220 - Thread-2 - DEBUG - Thread-2 wait...6036
2017-08-20 12:32:55,220 - Thread-2 - DEBUG - Thread-2 wait...6036
2017-08-20 12:32:55,221 - Thread-2 - DEBUG - Thread-2 wait...6036
2017-08-20 12:32:55,221 - Thread-2 - DEBUG - Thread-2 wait...6036
2017-08-20 12:32:55,221 - Thread-2 - DEBUG - Thread-2 wait...6036
2017-08-20 12:32:55,221 - Thread-2 - DEBUG - Thread-2 wait...6036
2017-08-20 12:32:55,226 - Thread-1 - DEBUG - Thread-1 finished, count is 13964
2017-08-20 12:32:55,236 - Thread-2 - DEBUG - Thread-2 finished, count is 20000
2017-08-20 12:32:55,236 - MainThread - DEBUG - final count: 20000
4.threading.RLock() 的应用场景
lock = threading.Lock() def get_first_part():
with lock: # any thread that attempts to acquire the lock will block, even if the same thread is already holding the lock.
... fetch data for first part from shared object
return data def get_second_part():
with lock:
... fetch data for second part from shared object
return data def get_both_parts():
with lock: # other thread may modify the resource between the two calls
first = get_first_part()
# between the two calls
second = get_second_part()
return first, second # While simple locks will block if the same thread attempts to acquire the same lock twice,
# a re-entrant lock only blocks if another thread currently holds the lock.
rlock = threading.RLock()
python之多线程 threading.Lock() 和 threading.RLock()的更多相关文章
- python的threading的使用(join方法,多线程,锁threading.Lock和threading.Condition
一.开启多线程方法一 import threading,time def write1(): for i in range(1,5): print('1') time.sleep(1) def wri ...
- python爬虫——多线程+协程(threading+gevent)
上一篇博客中我介绍了如何将爬虫改造为多进程爬虫,但是这种方法对爬虫效率的提升不是非常明显,而且占用电脑cpu较高,不是非常适用于爬虫.这篇博客中,我将介绍在爬虫中广泛运用的多线程+协程的解决方案,亲测 ...
- 基于Python的多线程模块Threading小结
步入正题前,先准备下基本知识,线程与进程的概念. 相信作为一个测试人员,如果从理论概念上来说其两者的概念或者区别,估计只会一脸蒙蔽,这里就举个例子来说明下其中的相关概念. 平安夜刚过,你是吃到了苹果还 ...
- Python:多线程编程
1.IO编程 IO(input/output).凡是用到数据交换的地方,都会涉及io编程,例如磁盘,网络的数据传输.在IO编程中,stream(流)是一种重要的概念,分为输入流(input strea ...
- 孤荷凌寒自学python第三十九天python 的线程锁Lock
孤荷凌寒自学python第三十九天python的线程锁Lock (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) 当多个线程同时操作一个文件等需要同时操作某一对象的情况发生时,很有可能发生冲突, ...
- Day12- Python基础12 线程、GIL、Lock锁、RLock锁、Semaphore锁、同步条件event
http://www.cnblogs.com/yuanchenqi/articles/6248025.html 博客地址 本节内容: 1:进程和线程的说明 2:线程的两种调用方式 3:threadi ...
- python中多线程相关
基础知识 进程:进程就是一个程序在一个数据集上的一次动态执行过程 数据集:程序执行过程中需要的资源 进程控制块:完成状态保存的单元 线程:线程是寄托在进程之上,为了提高系统的并发性 线程是进程的实体 ...
- python网络-多线程(22)
一.什么是线程 线程(英语:thread)是操作系统能够进行运算调度的最小单位.它被包含在进程之中,是进程中的实际运作单位.同一进程中的多条线程将共享该进程中的全部系统资源,一个进程可以有很多线程,每 ...
- python 使用多线程进行并发编程/互斥锁的使用
import threading import time """ python的thread模块是比较底层的模块,python的threading模块是对thread做了 ...
随机推荐
- 设计模式C++学习笔记之十八(Visitor访问者模式)
18.1.解释 概念:表示一个作用于某对象结构中的各元素的操作.它使你可以在不改变各元素的类的前提下定义作用于这些元素的新操作. main(),客户 IVisitor,访问者接口 CBaseVis ...
- js判断空字符串、null、undefined、空格、中文空格
代码 function isEmpty(obj) { if (obj === null) return true; if (typeof obj === 'undefined') { return t ...
- FreeSWITCH黑名单功能设置
功能描述:对呼叫的号码进行过滤 步骤: 1.编译mod_blacklist 模块:进入源目录/usr/local/src/freeswitch --> make mod_blacklist-i ...
- oracle里面用sql做报表并带小计合计常用到的函数
1-- DECODE函数是Oracle PL/SQL是功能强大的函数之一,假设我们想给职员加工资,其标准是:工资在8000元以下的将加20%:工资在8000元以上的加15%,通常的做法是,先选出记录 ...
- linux云主机cpu一直很高降不下来,系统日志报nf_conntrack: table full, dropping packet.
在启用了iptables web服务器上,流量高的时候经常会出现下面的错误: ip_conntrack: table full, dropping packet 这个问题的原因是由于web服务器收到了 ...
- Float与二进制之间的转化(Java实现)
在线转化:http://www.binaryconvert.com import java.text.DecimalFormat; public class SinglePrecision { //浮 ...
- fatal: refusing to merge unrelated histories
Git 提交代码时遇到冲突了,所以 git pull 拉不下来远程代码.使用一下命令解决: git pull origin master --allow-unrelated-histories 然后解 ...
- 解决Navicat连接MySQL总是报错1251的方法
今天下了个 MySQL8.0,发现Navicat连接不上,总是报错1251: 原因是MySQL8.0版本的加密方式和MySQL5.0的不一样,连接会报错. 试了很多种方法,终于找到一种可以实现的: 更 ...
- Java语法基础常见疑惑解答
1. 类是java的最小单位,java的程序必须在类中才能运行 2. java函数加不加static有何不同 java中声明为static的方法称为静态方法或类方法.静态方法可以直接调用静态方法,访问 ...
- laravel 频率限制throttle
在 Laravel 5.6 中,还引入了频率限制功能.所谓频率限制,指的是在指定时间单个用户对某个路由的访问次数限制,该功能有两个使用场景,一个是在某些需要验证/认证的页面限制用户失败尝试次数,提高系 ...