Threading模块是python3里面的多线程模块,模块内集成了许多的类,其中包括Thread,Condition,Event,Lock,Rlock,Semaphore,Timer等等。下面这篇文章主要通过案例来说明其中的Event和Segmaphore(Boundedsegmaphore)的使用。关于Lock的使用可以移步到我之前写的文章python同步原语--线程锁。

Event

Event类内部保存着一个flags参数,标志事件的等待与否。

Event类实例函数

1. set() 将flags设置为True,事件停止阻塞

2. clear()  将flags重新设置为False,删除flags,事件重新阻塞

3. wait() 将事件设置为等待状态

4.is_set()判断flags是否被设置,如果被设置返回True,否则返回False

(1)单个事件等待其他事件的发生

具体代码:

from time import ctime,sleep
event = Event() def event_wait():
print(ctime())
event.wait()
print('这是event_wait方法中的时间',ctime()) def event_set(n):
sleep(n)
event.set()
print('这是event_set方法中的时间', ctime()) thread1 = Thread(target=event_wait)
thread2 = Thread(target=event_set,args=(3,)) thread1.start()
thread2.start()

结果:

Sat Nov 17 10:01:05 2018
这是event_wait方法中的时间 Sat Nov 17 10:01:08 2018
这是event_set方法中的时间 Sat Nov 17 10:01:08 2018

(2)多个事件先后发生

下面以赛跑来作为例子。假设5条跑道上,每条跑道各有一名运动员,分别为ABCDE。

具体代码:

from threading import Event
from threading import Thread
import threading event = Event() def do_wait(athlete):
racetrack = threading.current_thread().getName()
print('%s准备就绪' % racetrack)
event.wait()
print('%s听到枪声,起跑!'%athlete) thread1 = Thread(target=do_wait,args=("A",))
thread2 = Thread(target=do_wait,args=("B",))
thread3 = Thread(target=do_wait,args=("C",))
thread4 = Thread(target=do_wait,args=("D",))
thread5 = Thread(target=do_wait,args=("E",)) threads = []
threads.append(thread1)
threads.append(thread2)
threads.append(thread3)
threads.append(thread4)
threads.append(thread5) for th in threads:
th.start() event.set() #这个set()方法很关键,同时对5个线程中的event进行set操作

结果:

Thread-1准备就绪
Thread-2准备就绪
Thread-3准备就绪
Thread-4准备就绪
Thread-5准备就绪
E听到枪声,起跑!
A听到枪声,起跑!
B听到枪声,起跑!
D听到枪声,起跑!
C听到枪声,起跑!

可以看出多个线程中event的set()是随机的,其内部的实现是因为一个notify_all()方法。这个方法会一次性释放所有锁住的事件,哪个线程先抢到线程运行的时间片,就先释放锁。

之所以能够只调用一个set()函数就可以实现所有event的退出阻塞,是因为event.wait()是在线程内部实现的,而set()函数是在进程中调用,python多线程共享一个进程内存空间。如果是在不同进程中调用这两个函数则无法实现。

BoundedSegmaphore

如果在主机执行IO密集型任务的时候再执行这种短时间内完成大量任务(多线程)的程序时,计算机就有很大可能会宕机。

这时候就可以为这段程序添加一个计数器(counter)功能,来限制一个时间点内的线程数量。当每次进行IO操作时,都需要向segmaphore请求资源(锁),如果没有请求到,就阻塞等待,请求成功才就像执行任务。

BoundedSegmaphore和Segmaphore的区别

BoundedSegmaphore请求的锁数量固定为传入参数,而Segmaphore请求的锁数量可以超过传入的参数。

主要函数:

1. acquire()  请求锁

2. release()   释放锁

下面以一个租房的例子来说明这种固定锁数量的机制。假设一家小公寓有6间房,原本有2个住户在住着。

具体代码实现:

from threading import BoundedSemaphore,Lock,Thread
from time import sleep
from random import randrange lock = Lock()
num = 6
hotel = BoundedSemaphore(num) def logout():
lock.acquire()
print('I want to logout')
print('A customer logout...')
try:
hotel.release()
print('Welcome again')
except ValueError:
print('Sorry,wait a moment.')
lock.release() def login():
lock.acquire()
print('I want to login')
print('A customer login...')
if hotel.acquire(False):
print('Ok,your room number is...')
else:
print('Sorry,our hotel is full')
lock.release() #房东
def producer(loops):
for i in range(loops):
logout()
print('还剩%s' % hotel._value, '房间')
sleep(randrange(2))
#租客
def consumer(loops):
for i in range(loops):
login()
print('还剩%s' % hotel._value, '房间')
sleep(randrange(2))
def main():
print('Start')
room_num = hotel._value
print('The hotel is full with %s room'%room_num)
#原本有2个住户
hotel.acquire()
hotel.acquire()
thread1 = Thread(target=producer,args=(randrange(2,8),))
thread2 = Thread(target=consumer,args=(randrange(2,8),))
thread1.start()
thread2.start() if __name__ == '__main__':
main()

结果:

The hotel is full with 6 room
I want to logout
A customer logout...
Welcome again
还剩5 房间
I want to logout
A customer logout...
Welcome again
还剩6 房间
I want to login
A customer login...
Ok,your room number is...
还剩5 房间
I want to login
A customer login...
Ok,your room number is...
还剩4 房间

可以看出,房间数目永远不会超过6,因为_value值(BoundedSegmaphore内部的计数器counter)一定是传入的参数6。

python--线程同步原语的更多相关文章

  1. python线程同步原语--源码阅读

    前面两篇文章,写了python线程同步原语的基本应用.下面这篇文章主要是通过阅读源码来了解这几个类的内部原理和是怎么协同一起工作来实现python多线程的. 相关文章链接:python同步原语--线程 ...

  2. C#并行编程-线程同步原语

    菜鸟学习并行编程,参考<C#并行编程高级教程.PDF>,如有错误,欢迎指正. 目录 C#并行编程-相关概念 C#并行编程-Parallel C#并行编程-Task C#并行编程-并发集合 ...

  3. Python线程同步

    线程执行 join与setDaemon 子线程在主线程运行结束后,会继续执行完,如果给子线程设置为守护线程(setDaemon=True),主线程运行结束子线程即结束: 如果join()线程,那么主线 ...

  4. Python 线程同步

    #-*-coding:utf-8-*- '''如果多个线程共同对某个数据修改,则可能出现不可预料的结果,为了保证数据的正确性, 需要对多个线程进行同步. 线程同步所使用的的方法: Lock RLock ...

  5. Python 线程同步变量,同步条件,列队

    条件变量同步 有一类线程需要满足条件之后才能够继续执行,Python提供了threading.Condition 对象用于条件变量线程的支持,它除了能提供RLock()或Lock()的方法外,还提供了 ...

  6. Python 线程同步锁, 信号量

    同步锁 import time, threading def addNum(): global num num -= 1 num = 100 thread_list = [] for i in ran ...

  7. python同步原语--线程锁

    多线程锁是python多种同步原语中的其中一种.首先解析一下什么是同步原语,python因为GIL(全局解析锁)的缘故,并没有真正的多线性.另外python的多线程存在一个问题,在多线程编程时,会出现 ...

  8. Python多线程(2)——线程同步机制

    本文介绍Python中的线程同步对象,主要涉及 thread 和 threading 模块. threading 模块提供的线程同步原语包括:Lock.RLock.Condition.Event.Se ...

  9. python——线程相关

    使用python的threading中的Thread 下面是两种基本的实现线程的方式: 第一种方式———— #coding=utf-8 """ thread的第一种声明及 ...

  10. python多线程同步机制Semaphore

    #!/usr/bin/env python # -*- coding: utf-8 -*- """ Python 线程同步机制:Semaphore "" ...

随机推荐

  1. win 10 升级远程连接服务器 要求的函数不受支持

    首先展示错误信息: win10更新系统后,之前连接的服务器都连接不上了,应该用一下方法解决: 运行 gpedit.msc,打开本地组策略:计算机配置>管理模板>系统>凭据分配> ...

  2. python高级-包(15)

    一.引入包 1.1 有2个模块功能有些联系 receiveMsg.py和sendMsg.py都在msg文件夹里面. 1.2.使用import 文件.模块的方式导入 在桌面创建demo.py文件,并把r ...

  3. 关于mysql的update、delete、和insert into能否使用别名问题

    在工作中遇到这样一个问题,就是mysql在insert into时能不能使用别名,大家会很奇怪为什么insert into使用别名呢?原因在于原来的项目中使用了user表,新项目要将user表拆分为u ...

  4. CentOS7.0小随笔——指令基本操作(Part.B)

    一.文件与目录基本操作指令 touch命令 在Linux中,touch指令可以建立一个空文件 但如果创建的文件本身存在(指在同一目录下),则会修改文件最后的访问时间,并不会更改文件内的内容. 例:# ...

  5. Code Complete-13/7/23

    What is "construction"?    Hava u ever  used construction paper to make some things?What i ...

  6. Java并发编程之AQS

    一.什么是AQS AQS(AbstractQueuedSynchronize:队列同步器)是用来构建锁或者其他同步组件的基础框架,很多同步类都是在它的基础上实现的,比如常用的ReentrantLock ...

  7. 一文带你看懂cookie,面试前端不用愁

    本文由云+社区发表 在前端面试中,有一个必问的问题:请你谈谈cookie和localStorage有什么区别啊? localStorage是H5中的一种浏览器本地存储方式,而实际上,cookie本身并 ...

  8. k8s部署kafka集群

    一.概述 在k8s里面部署kafka.zookeeper这种有状态的服务,不能使用deployment和RC,k8s提供了一种专门用来部署这种有状态的服务的API--statefulset,有状态简单 ...

  9. JVM中对象的回收过程

      当我们的程序开启运行之后就,就会在我们的java堆中不断的产生新的对象,而这是需要占用我们的存储空间的,因为创建一个新的对象需要分配对应的内存空间,显然我的内存空间是固定有限的,所以我们需要对没有 ...

  10. 第一册:lesson thirty three。

    原文:A fine day. It is a fine day today. There are some clouds in the sky. But the sun is shining. Mr. ...