Python线程同步的实现代码
作者:Harvard_Fly 发布时间:2022-10-01 00:20:48
本文介绍Python中的线程同步对象,主要涉及 thread 和 threading 模块。
threading 模块提供的线程同步原语包括:Lock、RLock、Condition、Event、Semaphore等对象。
线程执行
join与setDaemon
子线程在主线程运行结束后,会继续执行完,如果给子线程设置为守护线程(setDaemon=True),主线程运行结束子线程即结束;
如果join()线程,那么主线程会等待子线程执行完再执行。
import threading
import time
def get_thread_a():
print("get thread A started")
time.sleep(3)
print("get thread A end")
def get_thread_b():
print("get thread B started")
time.sleep(5)
print("get thread B end")
if __name__ == "__main__":
thread_a = threading.Thread(target=get_thread_a)
thread_b = threading.Thread(target=get_thread_b)
start_time = time.time()
thread_b.setDaemon(True)
thread_a.start()
thread_b.start()
thread_a.join()
end_time = time.time()
print("execution time: {}".format(end_time - start_time))
thread_a是join,首先子线程thread_a执行,thread_b是守护线程,当主线程执行完后,thread_b不会再执行执行结果如下:
get thread A started
get thread B started
get thread A end
execution time: 3.003199815750122
线程同步
当线程间共享全局变量,多个线程对该变量执行不同的操作时,该变量最终的结果可能是不确定的(每次线程执行后的结果不同),如:对count变量执行加减操作 ,count的值是不确定的,要想count的值是一个确定的需对线程执行的代码段加锁。
python对线程加锁主要有Lock和Rlock模块
Lock:
from threading import Lock
lock = Lock()
lock.acquire()
lock.release()
Lock有acquire()和release()方法,这两个方法必须是成对出现的,acquire()后面必须release()后才能再acquire(),否则会造成死锁
Rlock:
鉴于Lock可能会造成死锁的情况,RLock(可重入锁)对Lock进行了改进,RLock可以在同一个线程里面连续调用多次acquire(),但必须再执行相同次数的release()
from threading import RLock
lock = RLock()
lock.acquire()
lock.acquire()
lock.release()
lock.release()
condition(条件变量),线程在执行时,当满足了特定的条件后,才可以访问相关的数据
import threading
def get_thread_a(condition):
with condition:
condition.wait()
print("A : Hello B,that's ok")
condition.notify()
condition.wait()
print("A : I'm fine,and you?")
condition.notify()
condition.wait()
print("A : Nice to meet you")
condition.notify()
condition.wait()
print("A : That's all for today")
condition.notify()
def get_thread_b(condition):
with condition:
print("B : Hi A, Let's start the conversation")
condition.notify()
condition.wait()
print("B : How are you")
condition.notify()
condition.wait()
print("B : I'm fine too")
condition.notify()
condition.wait()
print("B : Nice to meet you,too")
condition.notify()
condition.wait()
print("B : Oh,goodbye")
if __name__ == "__main__":
condition = threading.Condition()
thread_a = threading.Thread(target=get_thread_a, args=(condition,))
thread_b = threading.Thread(target=get_thread_b, args=(condition,))
thread_a.start()
thread_b.start()
Condition内部有一把锁,默认是RLock,在调用wait()和notify()之前必须先调用acquire()获取这个锁,才能继续执行;当wait()和notify()执行完后,需调用release()释放这个锁,在执行with condition时,会先执行acquire(),with结束时,执行了release();所以condition有两层锁,最底层锁在调用wait()时会释放,同时会加一把锁到等待队列,等待notify()唤醒释放锁
wait() :允许等待某个条件变量的通知,notify()可唤醒
notify(): 唤醒等待队列wait()
执行结果:
B : Hi A, Let's start the conversation
A : Hello B,that's ok
B : How are you
A : I'm fine,and you?
B : I'm fine too
A : Nice to meet you
B : Nice to meet you,too
A : That's all for today
B : Oh,goodbye
Semaphore(信号量)
用于控制线程的并发数,如爬虫中请求次数过于频繁会被禁止ip,每次控制爬取网页的线程数量可在一定程度上防止ip被禁;文件读写中,控制写线程每次只有一个,读线程可多个。
import time
import threading
def get_thread_a(semaphore,i):
time.sleep(1)
print("get thread : {}".format(i))
semaphore.release()
def get_thread_b(semaphore):
for i in range(10):
semaphore.acquire()
thread_a = threading.Thread(target=get_thread_a, args=(semaphore,i))
thread_a.start()
if __name__ == "__main__":
semaphore = threading.Semaphore(2)
thread_b = threading.Thread(target=get_thread_b, args=(semaphore,))
thread_b.start()
上述示例了每隔1秒并发两个线程执行的情况,当调用一次semaphore.acquire()时,Semaphore的数量就减1,直至Semaphore数量为0时被锁上,当release()后Semaphore数量加1。Semaphore在本质上是调用的Condition,semaphore.acquire()在Semaphore的值为0的条件下会调用Condition.wait(), 否则将值减1,semaphore.release()会将Semaphore的值加1,并调用Condition.notify()
Semaphore源码
def acquire(self, blocking=True, timeout=None):
if not blocking and timeout is not None:
raise ValueError("can't specify timeout for non-blocking acquire")
rc = False
endtime = None
with self._cond:
while self._value == 0:
if not blocking:
break
if timeout is not None:
if endtime is None:
endtime = _time() + timeout
else:
timeout = endtime - _time()
if timeout <= 0:
break
self._cond.wait(timeout)
else:
self._value -= 1
rc = True
return rc
def release(self):
with self._cond:
self._value += 1
self._cond.notify()
来源:http://www.cnblogs.com/FG123/p/9704158.html


猜你喜欢
- 1. 安装clickClick 是 Flask 的开发团队 Pallets 的另一款开源项目,它是用于快速创建命令行的第三方模块。官网文档地
- MySQL中可以使用rename table这个SQL语句来修改表名。rename table这个SQL语句来修改表名的基本语法是:RENA
- 一、python-yml文件读写使用库 :import yaml安装:pip install pyyaml示例:文件config2.ymlg
- 本文实例为大家分享了python实现图像识别的具体代码,供大家参考,具体内容如下#! /usr/bin/env python from PI
- 大家都知道,数据库的安全性是很重要的,它直接影响到数据库的广泛应用。用户可以采用任意一种方法来保护数据库应用程序,也可以将几种方法结合起来使
- 1、将时间转换为字符串的形式: convert(varchar,字段名,120)--------yyyy-mm-ddhh:ss:mm0000
- 本文实例讲述了python命令行参数解析OptionParser类的用法,分享给大家供大家参考。具体代码如下:from optparse i
- mysql 5.5 安装配置方法图文教程回忆一下mysql 5.5 安装配置方法,整理mysql 5.5 安装配置教程笔记,分享给大家。My
- 如下所示:from pymongo import MongoClient#建立和数据库系统的连接,指定host及port参数client =
- 问题出现与解决Pandas进行数据处理之后,假如想将其转化为json,会出现一个bug,就是中文文字是以乱码存储的,也就是\uXXXXXX的
- 前言Python 读取数据自动写入 MySQL 数据库,这个需求在工作中是非常普遍的,主要涉及到 python 操作数据库,读写更新等,数据
- 由于GROUP BY 实际上也同样会进行排序操作,而且与ORDER BY 相比,GROUP BY 主要只是多了排序之后的分组操作。当然,如果
- 本文实例讲述了Yii框架引用插件和ckeditor中body与P标签去除的方法。分享给大家供大家参考,具体如下:在Yii中引用插件注:插件和
- Python类的继承在OOP(ObjectOrientedProgramming)程序设计中,当我们定义一个class的时候,可以从某个现有
- 在 Python 中对一个可迭代对象进行排序是很常见的一个操作,一般会用到 sorted() 函数num_list = [4, 2, 8,
- 1. iocgo简介习惯于Java或者C#开发的人应该对控制反转与依赖注入应该再熟悉不过了。在Java平台有鼎鼎大名的Spring框架,在C
- 目录关于Web服务器和应用服务器Python项目部署架构关于cgi、wsgi、uwsgi、http协议关于cgi、fastcgi、php-f
- 1. 作用将类方法转换为类属性,可以用 . 直接获取属性值或者对属性进行赋值2.实现方式使用property类来实现,也可以使用proper
- 本文实例讲述了Python实现的摇骰子猜大小功能小游戏。分享给大家供大家参考,具体如下:最近学习Python的随机数,逻辑判断,循环的用法,
- 1、下载gliffy-confluence-plugin-9.1.2.obr2、解压后找到GliffyLicenseManager.clas