Python线程同步和条件变量 !

作者: 14e61d025165 | 来源:发表于2019-06-03 15:15 被阅读0次

1、线程同步

** Python学习交流群:1004391443,这里有资源共享,技术解答,还有小编从最基础的Python资料到项目实战的学习资料都有整理,希望能帮助你更了解python,学习python**

! /usr/bin/evn python3# ---- coding: utf-8 ----

from threading import Lock,RLockimport threading

说明:# 1、用锁会影响性能(必然存在的)# 2、锁会引起死锁# 死锁两种常见的情况:# 1、lock.acquire() lock.acquire()两次使用锁而无释放# 2、资源竞争'''A(a,b) acquire(a) acquire(b) B(a,b) acquire(a) acquire(b)'''# 3、两次调用lock.acquire()# 解决办法:Python中引入了可重入的锁from threading import RLock# 说明:在同一个线程里面,可以连续调用多次acquire(),一定要注意acquire()调用要和release()调用次数一致''' def add(): global total global lock for i in range(1000000): lock.acquire() dosomething(lock) total += 1 lock.release() def dosomething(): lock.acquire() lock.release()

'''#锁解决数据不一致问题
total = 0lock = Lock()
def add(): global total global lock for i in range(1000000): lock.acquire() total += 1 lock.release()
def desc(): global total global lock for j in range(1000000): lock.acquire() total -= 1 lock.release()
threading1 = threading.Thread(target = add)threading2 = threading.Thread(target= desc)
threading1.start()threading2.start()
threading1.join()threading2.join()print(total)

解决可以多次使用acquire()问题

total = 0lock = RLock()
def add(): global total global lock for i in range(1000000): lock.acquire() lock.acquire() total += 1 lock.release() lock.release()
def desc(): global total global lock for j in range(1000000): lock.acquire() total -= 1 lock.release()
threading1 = threading.Thread(target = add)threading2 = threading.Thread(target= desc)
threading1.start()threading2.start()
threading1.join()threading2.join()print(total)

2、条件变量

! /usr/bin/evn python3# ---- coding: utf-8 ----

线程中的条件变量()# 通知天猫与小爱之间的来回切换

import threading

class XiaoAi(threading.Thread):# def init(self,lock):# super().init(name="小爱")# self.lock = lock## def run(self):# self.lock.acquire()# print("{}: 在".format(self.name))# self.lock.release()## self.lock.acquire()# print("{} : 好啊!".format(self.name))# self.lock.release()### class TianMao(threading.Thread):# def init(self,lock):# super().init(name="天猫精灵")# self.lock = lock## def run(self):# self.lock.acquire()# print("{} : 小爱同学".format(self.name))# self.lock.release()## self.lock.acquire()# print("{} : 我们来对古诗吧!".format(self.name))# self.lock.release()### if name == "main":# lock = threading.Lock()# xiaoai = XiaoAi(lock)# tianmao = TianMao(lock)## tianmao.start()# xiaoai.start()

wait() 等待某个条件变量的通知#notify() 通知调用了wait()方法的启动from threading import Condition

class XiaoAi(threading.Thread): def init(self,cond): super().init(name="小爱") self.cond = cond
def run(self): with self.cond: self.cond.wait() print("{}: 在".format(self.name)) self.cond.notify() self.cond.wait()
print("{} : 好啊!".format(self.name)) self.cond.notify() self.cond.wait()
print("{} : 君住长江尾!".format(self.name)) self.cond.notify() self.cond.wait()
print("{} : 共饮长江水!".format(self.name)) self.cond.notify() self.cond.wait()
print("{} : 此恨何时已!".format(self.name)) self.cond.notify() self.cond.wait()
print("{} : 定不负相思意!".format(self.name)) self.cond.notify() self.cond.wait()
class TianMao(threading.Thread): def init(self,cond): super().init(name="天猫精灵") self.cond = cond
def run(self): with self.cond:#也可以这样调用:self.cond.acquire()方法,但一定要self.cond.release()方法 print("{} : 小爱同学".format(self.name)) self.cond.notify() self.cond.wait()
print("{} : 我们来对古诗吧!".format(self.name)) self.cond.notify() self.cond.wait()
print("{} : 我住长江头!".format(self.name)) self.cond.notify() self.cond.wait()
print("{} : 日日思君不见君!".format(self.name)) self.cond.notify() self.cond.wait()
print("{} : 此水几时休!".format(self.name)) self.cond.notify() self.cond.wait()
print("{} : 只愿君心似我心!".format(self.name)) self.cond.notify() self.cond.wait()
if name == "main": cond = threading.Condition() xiaoai = XiaoAi(cond) tianmao = TianMao(cond)
#启动顺序很重要 #我们先要一定调用Condition with()方法,with就相当于调用 #在调用with之后 cond之后才能调用wait()或者notify()方法 #condition有两层锁,一把底层锁会在线程调用了wait方法的时候释放,上面的锁会在每次调用wait()的时候分配一把并放入condition等待队列中,等待notify()方法的唤醒。 xiaoai.start() tianmao.start()

相关文章

  • Python线程同步和条件变量 !

    1、线程同步 ** Python学习交流群:1004391443,这里有资源共享,技术解答,还有小编从最基础的Py...

  • C++面向对象多线程学习笔记_同步对象

    通常而言,同步变量和条件变量声明为全局,所以,多个线程可以访问它们。虽然这需要使用同步变量的所有线程提供了方便,但...

  • APUE//线程同步3

    使用条件变量进行线程同步 barrier,屏障

  • APR分析-线程同步篇

    APR分析-线程同步篇 在线程同步方面,Posix标准定义了3种同步模型,分别为互斥量、条件变量和读写锁。APR也...

  • Linux线程同步

    Linux下提供了多种方式来处理线程同步,最常用的是互斥锁、条件变量和信号量。 Linux线程同步-----互斥锁...

  • 线程同步——条件变量+锁

    参考博文:https://www.cnblogs.com/zhangxuan/p/6526854.html 简介 ...

  • 线程同步-条件变量解析

    概念 线程同步的方法有多种,互斥量、信号量、条件变量、读写锁等。互斥量在允许或阻塞对临界区的访问上是很有效的,线程...

  • 线程同步之条件变量

    在多线程编程中,线程的同步是一个宏观的概念,其实其内部的实现包含两个部分:一是互斥,二是线程之间的相互依赖关系。就...

  • C++实现 生产者消费者模型

    condition_variable条件变量可以用来实现线程同步,它必须与互斥量mutex配合使用。条件变量适用场...

  • [python] 线程间同步之条件变量Condition

    为什么需要条件变量 有了前面提到的互斥锁,为什么还需要条件变量呢,当然是由于有些复杂问题互斥锁搞不定了。Pytho...

网友评论

    本文标题:Python线程同步和条件变量 !

    本文链接:https://www.haomeiwen.com/subject/bwmcxctx.html