# 互斥锁同时只允许一个线程更改数据,而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. js读取解析JSON类型数据

    原文地址:http://www.ablanxue.com/prone_3691_1.html JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,采用完全独立 ...

  2. 1043 Is It a Binary Search Tree (25 分)

    A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...

  3. [UE4]为UStaticMeshComponent添加点击事件

    BlockMesh->OnClicked.AddDynamic(this, &APuzzleBlock::BlockClicked); //鼠标点击事件 BlockMesh->On ...

  4. ThinkPHP框架学习摘要

    框架在linux与win下区别 1.文件权限设置: 2.大小写不规范: 学习框架的基本思路 : 1.如何收入并配置框架: 2.Controller的命名规范与书写规范: 3.Model的命名规范与书写 ...

  5. Linux命令详解-Apache网站服务器配置和管理

    1.Apache网站服务器配置和管理 1.源码包安装 2.rpm包安装 rpm –a | grep httpd 3.启动服务 service httpd start 4.配置文件: /etc/http ...

  6. (转)linux查找技巧: find grep xargs

    在当前目录下所有.cpp文件中查找efg函数 find . -name "*.cpp" | xargs grep 'efg' xargs展开find获得的结果,使其作为grep的参 ...

  7. 第13章 TCP编程(3)_基于自定义协议的多进程模型

    5. 自定义协议编程 (1)自定义协议:MSG //自定义的协议(TLV:Type length Value) typedef struct{ //协议头部 ];//TLV中的T unsigned i ...

  8. html 更新

    HTML介绍 Web服务本质 import socket sk = socket.socket() sk.bind(("127.0.0.1", 8080)) sk.listen(5 ...

  9. python进度条

    #!/usr/bin/env python# -*- coding:utf-8 -*- import urllib url = "http://www.163.com/" #htm ...

  10. viewer 照片查看器

    viewer 照片查看器 效果: api: https://github.com/fengyuanchen/viewerjs#methods npm: npm install viewerjs 使用: ...