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类是一个计数信号量,必须由获取它的线程释放, 通常用于限制可以访问某些资源(物理或 ...
随机推荐
- TSubobjectPtr和C++传统指针的区别
转自:http://aigo.iteye.com/blog/2282142 主要有以下区别(1和2的前提条件要满足:指针所在的class必须是UObjcct的子类): 1,TSubobjectPtr指 ...
- Session的使用与Session的生命周期
1.HttpSession的方法 Object getAttribute(String); Enumeration<String> getAttributeNames(); long ge ...
- Spring bean注解配置(1)
Spring自带的@Component注解及扩展@Repository.@Service.@Controller,如图 在使用注解方式配置bean时,需要引进一个包: 使用方法: 1.为需要使用注解方 ...
- pip安装包(python安装gevent(win))
下载: https://www.lfd.uci.edu/~gohlke/pythonlibs/#greenlet greenlet greenlet-0.4.14-cp36-cp36m-win_amd ...
- 中国移动基于ARM/x86服务器的Ceph性能对比
2018年11月17日,Ceph中国行首次走进杭州,万众期待的杭州站沙龙如期而至,虽然杭州已经开始降温,阵雨不断,但活动现场依然热度爆表,杭州各大IT公司的从业人员和Ceph爱好者纷纷而来. 中国移动 ...
- 并发基础(六) 线程Thread类的start()和run()
start()和run()方法对于刚接触线程的人来说,会有点混淆,有点难理解,一般都会有以下疑问: 一.start( )方法 1.为什么需要start方法:它的作用是什么: start方法的作用就是将 ...
- 掩膜操作手写+API(第二天)
1.1首先是用到的理论知识: 上面是一个通用的公式,光知道上面写程序还是有点麻烦的,下面公式画的有点丑,可以表达我的观点. 1.2用到的知识点:可以边看程序边看用到的知识点: CV_Assert(); ...
- 查看shell环境下,网络是否连通-curl/ping
检查网络是否可用 curl www.baidu.com <!--STATUS OK--><html>...</html> ping www.baidu.com注意: ...
- PY3 周总结 第一周
1. Python的三大特点是什么? 答:解释型.动态类型(运行期间才做数据类型检查).强类型定义(一个变量只能存储一种类型的数据).[See More] 2. 应该使用Python2 还是 Pyth ...
- 循环神经网络(RNN)
1. 场景与应用 在循环神经网络可以用于文本生成.机器翻译还有看图描述等,在这些场景中很多都出现了RNN的身影. 2. RNN的作用 传统的神经网络DNN或者CNN网络他们的输入和输出都是 ...