Python 限制线程的最大数量的方法(Semaphore)
作者:houyanhua1 时间:2022-03-02 06:24:09
如下所示:
import threading
import time
sem=threading.Semaphore(4) #限制线程的最大数量为4个
def gothread():
with sem: #锁定线程的最大数量
for i in range(8):
print(threading.current_thread().name,i)
time.sleep(1)
for i in range(5):
threading.Thread(target=gothread).start()
来源:https://blog.csdn.net/houyanhua1/article/details/78229034
标签:Python,线程,数量,Semaphore
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
用Python制作一个文件加密器
2023-01-16 02:41:05
![](https://img.aspxhome.com/file/2023/7/102737_0s.jpg)
JavaScript定时器实现的原理分析
2023-08-23 10:24:16
![](https://img.aspxhome.com/file/2023/4/56094_0s.jpg)
python去掉行尾的换行符方法
2021-10-24 02:36:22
Python快速从注释生成文档的方法
2022-07-11 04:55:37
![](https://img.aspxhome.com/file/2023/2/78642_0s.png)
python中Requests发送json格式的post请求方法
2021-05-24 10:09:45
![](https://img.aspxhome.com/file/2023/3/66773_0s.png)
如何爬取通过ajax加载数据的网站
2022-05-03 15:06:01
![](https://img.aspxhome.com/file/2023/7/94077_0s.png)
简单的网站页面有什么好处
2007-11-15 06:28:00
python判定文件目录是否存在及创建多层目录
2022-08-12 09:39:03
python画立方体--魔方
2022-04-22 10:20:43
![](https://img.aspxhome.com/file/2023/8/117468_0s.png)
Python抛出引发异常(raise)知识点总结
2023-09-12 06:56:01
6款jQuery图表插件[译]
2009-06-01 10:34:00
![](https://img.aspxhome.com/file/UploadPic/20096/1/08200312t-81s.png)
SQL Server 2000安装故障
2008-01-22 19:10:00
详解python使用canvas实现移动并绑定键盘
2022-08-18 01:02:52
![](https://img.aspxhome.com/file/2023/8/68368_0s.png)
Python读取Excel数据实现批量生成PPT
2021-03-05 23:57:35
![](https://img.aspxhome.com/file/2023/7/76987_0s.jpg)
python 装饰器详解与应用范例
2022-03-18 01:06:55
在Python中使用itertools模块中的组合函数的教程
2023-11-06 16:31:36
python3从网络摄像机解析mjpeg http流的示例
2021-01-12 09:00:54
PHP中文处理 中文字符串截取(mb_substr)和获取中文字符串字数
2023-07-11 03:55:49
Python基础之进程详解
2023-07-10 23:58:43
![](https://img.aspxhome.com/file/2023/9/103149_0s.jpg)
手机网站开发必修课[2009总结版]
2010-01-05 17:02:00
![](https://img.aspxhome.com/file/UploadPic/20101/11/nj0d3ofg-63s.jpg)