用Queue控制python多线程并发数量
python多线程如果不进行并发数量控制,在启动线程数量多到一定程度后,会造成线程无法启动的错误。
下面介绍用Queue控制多线程并发数量的方法(python3).
# -*- coding: utf-8 -*-
import threading
import Queue
import random
import time maxThreads = 3 class store(threading.Thread):
def __init__(self, store, queue):
threading.Thread.__init__(self)
self.queue = queue
self.store = store def run(self):
try:
time.sleep(random.randint(1,3))
print('This is store %s' % self.store)
except Exception as e:
print(e)
finally:
self.queue.get()
self.queue.task_done() def main():
q = Queue.Queue(maxThreads)
for s in range(15):
q.put(s)
t = store(s, q)
t.start()
q.join()
print('over') if __name__ == '__main__':
main()
This is store 1
This is store 3
This is store 0
This is store 2
This is store 4
This is store 6
This is store 5
This is store 7
This is store 8
This is store 9
This is store 11
This is store 13
This is store 10
This is store 12
This is store 14
over
>>>
用Queue控制python多线程并发数量的更多相关文章
- python 多线程并发threading & 任务队列Queue
https://docs.python.org/3.7/library/concurrency.htmlpython程序默认是单线程的,也就是说在前一句语句执行完之前后面的语句不能继续执行先感受一下线 ...
- Python 多线程并发程序设计与分析
多线程并发程序设计与分析 by:授客 QQ:1033553122 1.技术难点分析与总结 难点1:线程运行时,运行顺序不固定 难点2:同一段代码,再不加锁的情况下,可能被多个线程同时执行,这会造成很多 ...
- Python多线程并发的误区
由于项目要做一个并发测试,由于断言的东西较多,决定手写脚本.于是用python写了脚本: def test_method(thread_no): print("%s===test_metho ...
- python多线程并发
# coding=utf8 # 使用前需安装net-snmp-utils或net-snmp包 from _utils.patrol2 import run_cmd import sys import ...
- python 多线程 并发socket实例
sever side: import socketserver class MyTCPHandler(socketserver.BaseRequestHandler): def handle(self ...
- 【Python】 多线程并发threading & 任务队列Queue
threading python程序默认是单线程的,也就是说在前一句语句执行完之前后面的语句不能继续执行(不知道我理解得对不对) 先感受一下线程,一般情况下: def testa(): sleep(1 ...
- Python 多线程教程:并发与并行
转载于: https://my.oschina.net/leejun2005/blog/398826 在批评Python的讨论中,常常说起Python多线程是多么的难用.还有人对 global int ...
- Python中Queue模块及多线程使用
Python的Queue模块提供一种适用于多线程编程的FIFO实现.它可用于在生产者(producer)和消费者(consumer)之间线程安全(thread-safe)地传递消息或其它数据,因此多个 ...
- python多进程并发和多线程并发和协程
为什么需要并发编程? 如果程序中包含I/O操作,程序会有很高的延迟,CPU会处于等待状态,这样会浪费系统资源,浪费时间 1.Python的并发编程分为多进程并发和多线程并发 多进程并发:运行多个独立的 ...
随机推荐
- netty例子-客户端每隔5秒发送查询时间的请求,服务器端响应请求
netty是jboss公司开发的,基于异步的.事件驱动的网络应用程序框架,快速开发高性能.高可靠性的服务器和客户端程序 public class TimeServer { ; public void ...
- [Docker] - 使用 Kitematic 安装 elasticsearch 失败 之解决
环境:Docker v19.03.4 + Kitematic Issue:(HTTP code 404) no such image - no such image: elasticsearch:la ...
- day07——数据类型补充、坑、二次编码
day07 数据类型补充 str 首字母大写:capitalize() name = 'alex' name1 = name.capitalize() print(name1) 每个单词首字母大写:t ...
- 配置安全web服务
为站点 http://system1.group8.example.com 配置TLS加密: 1.一个已签名证书从 http://server.group8.example.com/pub/tls/c ...
- BZOJ3514 / Codechef GERALD07 Chef and Graph Queries LCT、主席树
传送门--BZOJ 传送门--VJ 考虑使用LCT维护时间最大生成树,那么对于第\(i\)条边,其加入时可能会删去一条边.记\(pre_i\)表示删去的边的编号,如果不存在则\(pre_i = 0\) ...
- jQuery.Form.js使用方法
一.jQuery.Form.js 插件的作用是实现Ajax提交表单. 方法: 1.formSerilize() 用于序列化表单中的数据,并将其自动整理成适合AJAX异步请求的URL地址格式. 2.cl ...
- AWS--Lamdba
分享一个Lambda相关的连接 https://blog.csdn.net/m0_37204491/article/details/72829477
- Java 之 JSP
一.JSP 概述 Java Server Pages:java 服务器页面.页面中既可以指定定义 html标签,也可以定义 Java 代码. 二.原理 JSP 本质上就是一个 Servlet. 原理示 ...
- Java 格式化日期、时间
有三种方法可以格式化日期.时间. 1.使用DateFormat类 获取DateFormat实例: DateFormat.getDateInstance() 只能格式化日期 2019年5 ...
- 个人项目WC(Python实现)
一.GitHub地址 https://github.com/hoka-17/WC 二.PSP表格 PSP2.1 Personal Software Process Stages 预估耗时(分钟) 实际 ...