# 互斥锁同时只允许一个线程更改数据,而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】的更多相关文章

  1. python线程信号量semaphore(33)

    通过前面对 线程互斥锁lock /  线程事件event / 线程条件变量condition / 线程定时器timer 的讲解,相信你对线程threading模块已经有了一定的了解,同时执行多个线程的 ...

  2. C# 多线程之一:信号量Semaphore

    通过使用一个计数器对共享资源进行访问控制,Semaphore构造器需要提供初始化的计数器(信号量)大小以及最大的计数器大小 访问共享资源时,程序首先申请一个向Semaphore申请一个许可证,Sema ...

  3. 经典线程同步 信号量Semaphore

    阅读本篇之前推荐阅读以下姊妹篇: <秒杀多线程第四篇一个经典的多线程同步问题> <秒杀多线程第五篇经典线程同步关键段CS> <秒杀多线程第六篇经典线程同步事件Event& ...

  4. 互斥锁Mutex与信号量Semaphore的区别

    转自互斥锁Mutex与信号量Semaphore的区别 多线程编程中,常常会遇到这两个概念:Mutex和Semaphore,两者之间区别如下: 有人做过如下类比: Mutex是一把钥匙,一个人拿了就可进 ...

  5. 信号量 Semaphore

    一.简介         信号量(Semaphore),有时被称为信号灯,是在多线程环境下使用,负责协调各个线程, 以保证它们能够正确.合理的使用公共资源. Semaphore可以控制某个资源可被同时 ...

  6. windows核心编程-信号量(semaphore)

    线程同步的方式主要有:临界区.互斥区.事件.信号量四种方式. 前边讲过了互斥器线程同步-----windows核心编程-互斥器(Mutexes),这章我来介绍一下信号量(semaphore)线程同步. ...

  7. 秒杀多线程第八篇 经典线程同步 信号量Semaphore

    阅读本篇之前推荐阅读以下姊妹篇: <秒杀多线程第四篇一个经典的多线程同步问题> <且不超过最大资源数量. 第三个參数能够用来传出先前的资源计数,设为NULL表示不须要传出. 注意:当 ...

  8. 转:【Java并发编程】之二十三:并发新特性—信号量Semaphore(含代码)

    载请注明出处:http://blog.csdn.net/ns_code/article/details/17524153 在操作系统中,信号量是个很重要的概念,它在控制进程间的协作方面有着非常重要的作 ...

  9. 多线程面试题系列(8):经典线程同步 信号量Semaphore

    前面介绍了关键段CS.事件Event.互斥量Mutex在经典线程同步问题中的使用.本篇介绍用信号量Semaphore来解决这个问题. 首先也来看看如何使用信号量,信号量Semaphore常用有三个函数 ...

  10. java笔记--对信号量Semaphore的理解与运用

    java Semaphore 信号量的使用: 在java中,提供了信号量Semaphore的支持. Semaphore类是一个计数信号量,必须由获取它的线程释放, 通常用于限制可以访问某些资源(物理或 ...

随机推荐

  1. testNG断言

    https://junit.org/junit4/javadoc/latest/org/junit/Assert.html#assertThat 断言:Hamcrest - Matchers 对象: ...

  2. mysql 不同事务隔离级别

    repeatable read 在同一事务中,同一查询多次进行时候,由于其他插入操作(insert)的事务提交,导致每次返回不同的结果集. 标准的repeatable read是允许幻读的,因为这一级 ...

  3. Windows下MySQL免安装版的安装、卸载

    一.安装 1.下载 到MySQL官网http://dev.mysql.com/downloads/mysql/ 下载mysql-5.6.15-win32.zip. 2.拷贝 将mysql-5.6.15 ...

  4. (转)android系统开发 AP 和 BP 简要说明

    手机的AP和BP根据上下文可以指代硬件和软件两种意思.  1) 大多数的手机都含有两个处理器.操作系统.用户界面和应用程序都在Application Processor(AP)上执行,AP一般采用AR ...

  5. PHP写日志公共类

    Txl_Log.php <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); /** * * * ...

  6. nginx, supervisor

    Nginx(单进程): 反向代理, 负载均衡.图解 将配置文件 nginx.conf 的 user xx 配置好 xx用户 检查语法 $ sudo service nginx configtest 重 ...

  7. loganalyzer

  8. leetcode965

    public class Solution { List<int> list = new List<int>(); private void postTree(TreeNode ...

  9. java求两个数百分比,精确到指定位数

    // 获取百分比,不带小数点 private String getPercentage(String num, String total){ NumberFormat numberFormat = N ...

  10. as3 中 final 修饰符

    现在,在ActionScript 3.0的修饰符中,只有final修饰符没有介绍.之所有放在这里介绍,是因为final修饰符只与继承有关,指定一个方法不能被重写或一个类不能被继承. 一般来说,当用fi ...