python之信号量【Semaphore】
# 互斥锁同时只允许一个线程更改数据,而Semaphore是同时允许一定数量的线程更改数据,比如
# 一个厕所有3个坑,那么最多只允许3个人上厕所,后面的人只能等里面有人出来了才能再进去 import threading
import time def run(n):
semaphore.acquire()
time.sleep(1)
print("run the thread: %s" %n)
semaphore.release() if __name__ == '__main__':
num = 0
semaphore = threading.BoundedSemaphore(3)
#最多允许3个线程同时运行
for i in range(20):
t = threading.Thread(target=run,args=[i,])
t.start() while threading.active_count() != 1:
print(threading.active_count())
pass
else:
print("----all threads done----------")
print(num)
下面我们来详细的讲解下信号量的例子,先看下测试代码
import threading
import time def run(n):
semaphore.acquire()
time.sleep(1)
print("run the thread: %s" % n)
semaphore.release() if __name__ == '__main__':
num = 0
semaphore = threading.BoundedSemaphore(2)
# 最多允许3个线程同时运行
for i in range(6):
# print(i)
t = threading.Thread(target=run, args=[i, ])
t.start() while threading.active_count() != 1:
time.sleep(0.5)
print(threading.active_count())
else:
print("----all threads done----------")
我们会打印出当前active的线程数,这里需要注意,这个线程数还包括我们的主进程,也就是我们这里通过主进程起了6个子线程,那么他的active的线程数为7
我们看下打印的结果
7
run the thread: 1
run the thread: 0
5
5
run the thread: 2
run the thread: 3
3
3
run the thread: 4
run the thread: 5
1
----all threads done----------
通过上面的结果,我们可以看到active的线程数开始为7个,因为我们一共有7个线程,只要线程被start了,该线程就是active状态的
然后线程数从7个变为5个,在变为3个,在变为1个,最后为0个,这样我们就可以清晰的看到,同时只有2个线程可以在运行
我们下面把信号量调整为3个
import threading
import time def run(n):
semaphore.acquire()
time.sleep(1)
print("run the thread: %s" % n)
semaphore.release() if __name__ == '__main__':
num = 0
semaphore = threading.BoundedSemaphore(3)
# 最多允许3个线程同时运行
for i in range(6):
# print(i)
t = threading.Thread(target=run, args=[i, ])
t.start() while threading.active_count() != 1:
time.sleep(0.5)
print(threading.active_count())
else:
print("----all threads done----------")
结果如下
7
run the thread: 1
run the thread: 0
run the thread: 2
4
4
run the thread: 5
run the thread: 3
run the thread: 4
1
----all threads done----------
最后我们不设置信号量
测试代码如下
import threading
import time def run(n):
# semaphore.acquire()
time.sleep(1)
print("run the thread: %s" % n)
# semaphore.release() if __name__ == '__main__':
num = 0
semaphore = threading.BoundedSemaphore(3)
# 最多允许3个线程同时运行
for i in range(6):
# print(i)
t = threading.Thread(target=run, args=[i, ])
t.start() while threading.active_count() != 1:
time.sleep(0.5)
print(threading.active_count())
else:
print("----all threads done----------")
结果如下
7
run the thread: 0
run the thread: 2
run the thread: 3
run the thread: 5
run the thread: 1
run the thread: 4
1
----all threads done----------
现在应该小伙伴们对python多线程的信号量应该掌握了把
python之信号量【Semaphore】的更多相关文章
- python线程信号量semaphore(33)
通过前面对 线程互斥锁lock / 线程事件event / 线程条件变量condition / 线程定时器timer 的讲解,相信你对线程threading模块已经有了一定的了解,同时执行多个线程的 ...
- C# 多线程之一:信号量Semaphore
通过使用一个计数器对共享资源进行访问控制,Semaphore构造器需要提供初始化的计数器(信号量)大小以及最大的计数器大小 访问共享资源时,程序首先申请一个向Semaphore申请一个许可证,Sema ...
- 经典线程同步 信号量Semaphore
阅读本篇之前推荐阅读以下姊妹篇: <秒杀多线程第四篇一个经典的多线程同步问题> <秒杀多线程第五篇经典线程同步关键段CS> <秒杀多线程第六篇经典线程同步事件Event& ...
- 互斥锁Mutex与信号量Semaphore的区别
转自互斥锁Mutex与信号量Semaphore的区别 多线程编程中,常常会遇到这两个概念:Mutex和Semaphore,两者之间区别如下: 有人做过如下类比: Mutex是一把钥匙,一个人拿了就可进 ...
- 信号量 Semaphore
一.简介 信号量(Semaphore),有时被称为信号灯,是在多线程环境下使用,负责协调各个线程, 以保证它们能够正确.合理的使用公共资源. Semaphore可以控制某个资源可被同时 ...
- windows核心编程-信号量(semaphore)
线程同步的方式主要有:临界区.互斥区.事件.信号量四种方式. 前边讲过了互斥器线程同步-----windows核心编程-互斥器(Mutexes),这章我来介绍一下信号量(semaphore)线程同步. ...
- 秒杀多线程第八篇 经典线程同步 信号量Semaphore
阅读本篇之前推荐阅读以下姊妹篇: <秒杀多线程第四篇一个经典的多线程同步问题> <且不超过最大资源数量. 第三个參数能够用来传出先前的资源计数,设为NULL表示不须要传出. 注意:当 ...
- 转:【Java并发编程】之二十三:并发新特性—信号量Semaphore(含代码)
载请注明出处:http://blog.csdn.net/ns_code/article/details/17524153 在操作系统中,信号量是个很重要的概念,它在控制进程间的协作方面有着非常重要的作 ...
- 多线程面试题系列(8):经典线程同步 信号量Semaphore
前面介绍了关键段CS.事件Event.互斥量Mutex在经典线程同步问题中的使用.本篇介绍用信号量Semaphore来解决这个问题. 首先也来看看如何使用信号量,信号量Semaphore常用有三个函数 ...
- java笔记--对信号量Semaphore的理解与运用
java Semaphore 信号量的使用: 在java中,提供了信号量Semaphore的支持. Semaphore类是一个计数信号量,必须由获取它的线程释放, 通常用于限制可以访问某些资源(物理或 ...
随机推荐
- 1084 Broken Keyboard (20 分)
1084 Broken Keyboard (20 分) On a broken keyboard, some of the keys are worn out. So when you type so ...
- 搭建OpenStack先电云平台
实际操作示意图 在VMware里面创建两台centos7的虚拟机作为搭建云平台的两节点配置如下: 1.第一台虚拟机 作为控制节点 2CPU 3G以上内存 硬盘50G 网络适配器一个nat 一个仅主 ...
- mysql5.5版本以后插入中午显示问号的解决办法
先看看中午变问号的结果 现在看看我们建立数据库和建表的操作 看到这里相信大家都知道创建成功了,没错,数据库跟表是创建成功了,可当你录入的信息带中文的时候就显示问号. 现在用传统的解决办法 在查看下表的 ...
- 手贱!使用django,在数据库直接删除了表
莫名其妙的错误. 删除了migreation文件,并且更换了数据库. 1.直接makemigrations + migrate error: no change ?? WTF 2.makemi ...
- python学习之RabbitMQ-----消息队列
RabbitMQ队列 首先我们在讲rabbitMQ之前我们要说一下python里的queue:二者干的事情是一样的,都是队列,用于传递消息 在python的queue中有两个一个是线程queue,一个 ...
- Nop权限的使用
1.首先后台Admin--->siteMap中添加: <siteMapNode SystemName="我是系统名" nopResource="显示的中文名称 ...
- 49. jdk-6u45-linux-i586.bin安装步骤
# chmod u+x ./jdk-6u45-linux-i586.bin 1.# ./jdk-6u45-linux-i586.bin 在按提示输入yes后,jdk被解压到./jdk1.6.0_45目 ...
- springboot email 中常量值 配置 mailUtils
列如:邮件配置: application-test.properties #################Email config start############################ ...
- Devexpress 百分号显示格式
百分号:{0:P}表示显示为百分号模式.如数据源中为0.5.表示出来为50%
- 4. mybatis实战教程(mybatis in action)之四:实现关联数据的查询
转自:https://www.cnblogs.com/shanheyongmu/p/5653599.html 有了前面几章的基础,对一些简单的应用是可以处理的,但在实际项目中,经常是关联表的查询,比如 ...