Python线程同步
线程执行
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()
Python线程同步的更多相关文章
- python线程同步原语--源码阅读
前面两篇文章,写了python线程同步原语的基本应用.下面这篇文章主要是通过阅读源码来了解这几个类的内部原理和是怎么协同一起工作来实现python多线程的. 相关文章链接:python同步原语--线程 ...
- Python 线程同步
#-*-coding:utf-8-*- '''如果多个线程共同对某个数据修改,则可能出现不可预料的结果,为了保证数据的正确性, 需要对多个线程进行同步. 线程同步所使用的的方法: Lock RLock ...
- Python 线程同步变量,同步条件,列队
条件变量同步 有一类线程需要满足条件之后才能够继续执行,Python提供了threading.Condition 对象用于条件变量线程的支持,它除了能提供RLock()或Lock()的方法外,还提供了 ...
- Python 线程同步锁, 信号量
同步锁 import time, threading def addNum(): global num num -= 1 num = 100 thread_list = [] for i in ran ...
- python——线程相关
使用python的threading中的Thread 下面是两种基本的实现线程的方式: 第一种方式———— #coding=utf-8 """ thread的第一种声明及 ...
- python多线程同步机制Semaphore
#!/usr/bin/env python # -*- coding: utf-8 -*- """ Python 线程同步机制:Semaphore "" ...
- Python多线程(2)——线程同步机制
本文介绍Python中的线程同步对象,主要涉及 thread 和 threading 模块. threading 模块提供的线程同步原语包括:Lock.RLock.Condition.Event.Se ...
- PYTHON线程知识再研习E---条件变量同步Condition
Python提供的Condition对象提供了对复杂线程同步问题的支持.Condition被称为条件变量,除了提供与Lock类似的 acquire和release方法外,还提供了wait和notify ...
- python lock, semaphore, event实现线程同步
lock 机制不管你是java, C#, 还是python都是常用的线程同步机制, 相比较C# 的锁机制, python的加锁显得比较简单, 直接调用threading 标准库的lock 就可以了. ...
随机推荐
- postgresql 触发器 更新操作
1 前言 功能需求:当一张表格某个字段变化,另一张表某个字段写入该值 2 代码 CREATE OR REPLACE FUNCTION "public"."synStatu ...
- hive学习03-求一年中的最大温度
知识点: substr.concat函数的使用: row_number() over(distribute by year sort by temp desc) #按照年分组,按照tmp去排序 需 ...
- Python中加入中文注释
最近开发学习Pyton,当加入中文注释时,运行程序报错: File SyntaxError: Non-ASCII character , but no encoding declared; see h ...
- 点击<a>标签后禁止页面跳至顶部
一.点击<a>标签后禁止页面跳至顶部 1. 使用 href="javascript:void(0);",例如: <a href="javascript: ...
- Confluence 6 基本性能问题诊断步骤
基本性能问题诊断步骤 开始下面的程序: 进入 Troubleshooting Confluence hanging or crashing页面找到已知的主要性能问题. 进行页面 Performance ...
- Confluence 6 外部参考
一个外部参考的意思是任何站点链接到你 Confluence 的实例.任何时候当 Confluence 的用户单击这个外部链接的时候,Confluence 可以记录这次单击为参考. 在默认的情况下,外部 ...
- java-HTML&javaSkcript&CSS&jQuery&ajax( 八)
一.JavaScript教程笔记 1.在web页面中一般使用JavaScript脚本语言,支持跨平台,跨浏览器,驱动网页,与用户交互.另外Node.js把JavaScript引入到了服务器端. Jav ...
- Python之yield简明详解
yield在Python中被称之为生成器(只能在函数中使用),他的作用是将函数中每次执行的结果以类似元组的形式保存起来一遍后续使用. 什么是生成器? 通过列表生成式,我们可以直接创建一个列表.但是,受 ...
- Java 画一个5X5的方形矩阵
效果图如下: 思路:创建一个窗口,使其居中于屏幕中央,使用drawRect(x, y, width, height)画正方形. import java.awt.Graphics; import jav ...
- 3月9日(用 DBHelper 工具连接 mysql 数据库 实现登录验证)
一. 用DBHelper 与mysql 连接 实现最简单的登录验证. (1)新建 web project ----->选择src导入 DBHelper 工具包-------->选择web ...