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类是一个计数信号量,必须由获取它的线程释放, 通常用于限制可以访问某些资源(物理或 ...
随机推荐
- js读取解析JSON类型数据
原文地址:http://www.ablanxue.com/prone_3691_1.html JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,采用完全独立 ...
- Mybatis 接口绑定
MyBatis的接口绑定: 参考链接:http://blog.csdn.net/chris_mao/article/details/48836039 接口映射就是在IBatis中任意定义接口,然后把接 ...
- unity3d之Editor的Assembly-CSharp.dll文件路径
在Editor中与自己project中使用的Mono与Managed文件夹路径区别: Editor中:在unity安装路径[AppDir]下: 自己project中:在project的路径下,由bui ...
- ECharts之饼图和柱形图demo
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...
- 给iOS开发新手送点福利,简述UIScrollView的属性和用法
UIScrollView 1. contentOffset 默认CGPointZero,用来设置scrollView的滚动偏移量. // 设置scrollView的滚动偏移量 scrollView ...
- python之ConfigParser
以前傻傻的不知道还有configParser这么方便的模块,都是一个个的解析转换…… 配置文件xxxxx # 注释1 ; 注释2 [section1] # 节点 k1 = v1 # 值 k2: ...
- C# 程序A发送Log记录给程序B,程序B处理和分析Log记录
C# 程序A发送Log记录给程序B,程序B处理和分析Log记录 关键字:C# ;Log记录 ;在线Log记录;Socket:httplistener 一.常用场景 1. APP开发,在真机或者虚拟机上 ...
- ngnix配置thinkphp5隐藏index.php的方法亲测有效
在需要访问的域名的conf文件中,比如 vim /etc/nginx/.com.conf location / { // …..省略部分代码 if (!-e $request_filename) { ...
- 12.mysql高级查询
1. mysql 支持三种类型的连接查询: on 后面跟的是关联条件 内连接查询 select s.name,c.name from students as s inner join classes ...
- windows2008r2共享文件夹设置方法
一,无法启用网络发现的方法 参考网站: http://www.jb51.net/os/windows/win2008/154631.html Function Discovery R ...