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多线程并发数量的更多相关文章

  1. python 多线程并发threading & 任务队列Queue

    https://docs.python.org/3.7/library/concurrency.htmlpython程序默认是单线程的,也就是说在前一句语句执行完之前后面的语句不能继续执行先感受一下线 ...

  2. Python 多线程并发程序设计与分析

    多线程并发程序设计与分析 by:授客 QQ:1033553122 1.技术难点分析与总结 难点1:线程运行时,运行顺序不固定 难点2:同一段代码,再不加锁的情况下,可能被多个线程同时执行,这会造成很多 ...

  3. Python多线程并发的误区

    由于项目要做一个并发测试,由于断言的东西较多,决定手写脚本.于是用python写了脚本: def test_method(thread_no): print("%s===test_metho ...

  4. python多线程并发

    # coding=utf8 # 使用前需安装net-snmp-utils或net-snmp包 from _utils.patrol2 import run_cmd import sys import ...

  5. python 多线程 并发socket实例

    sever side: import socketserver class MyTCPHandler(socketserver.BaseRequestHandler): def handle(self ...

  6. 【Python】 多线程并发threading & 任务队列Queue

    threading python程序默认是单线程的,也就是说在前一句语句执行完之前后面的语句不能继续执行(不知道我理解得对不对) 先感受一下线程,一般情况下: def testa(): sleep(1 ...

  7. Python 多线程教程:并发与并行

    转载于: https://my.oschina.net/leejun2005/blog/398826 在批评Python的讨论中,常常说起Python多线程是多么的难用.还有人对 global int ...

  8. Python中Queue模块及多线程使用

    Python的Queue模块提供一种适用于多线程编程的FIFO实现.它可用于在生产者(producer)和消费者(consumer)之间线程安全(thread-safe)地传递消息或其它数据,因此多个 ...

  9. python多进程并发和多线程并发和协程

    为什么需要并发编程? 如果程序中包含I/O操作,程序会有很高的延迟,CPU会处于等待状态,这样会浪费系统资源,浪费时间 1.Python的并发编程分为多进程并发和多线程并发 多进程并发:运行多个独立的 ...

随机推荐

  1. css3写下雨效果

    css3写下雨效果<pre><div class="xiayuxiaoguo"></div></pre> <pre>.x ...

  2. SQL Server 类似正则表达式的字符处理问题

    SQL Serve提供了简单的字符模糊匹配功能,比如:like, patindex,不过对于某些字符处理场景还显得并不足够,日常碰到的几个问题有: 1. 同一个字符/字符串,出现了多少次 2. 同一个 ...

  3. [转帖]期待下一个十年|CIS 2019温馨回顾(附PPT下载)

    期待下一个十年|CIS 2019温馨回顾(附PPT下载) https://www.freebuf.com/fevents/222236.html shidongqi2019-12-06共26587人围 ...

  4. 资源池-数据库连接池简单实现-JAVA版本

    转载:https://www.jianshu.com/p/381c86bdbff6 看了看druid和dbcp2的原码,发现他们都有各自存储连接的方式. druid : private volatil ...

  5. C语言 hello

    #include <stdio.h> int main() { /* 我的第一个 C 程序 */ printf("Hello, World! \n"); ; } 实例解 ...

  6. Python知识点总结篇(五)

    软件目录结构规范 目标: 提高可读性: 提高可维护性: 常见结构 Demo/ |-- bin/ #存放项目的一些可执行文件 | |-- demo #可执行程序,启动demo调main.py | |-- ...

  7. sublime text 疑难解决

    sublime text 白色边框方框解决方法 https://blog.csdn.net/weixin_43228019/article/details/82766316 Sublime Text提 ...

  8. centos7中mysql的rpm包安装

    解决依赖 yum remove mysql-libs 执行命令:yum -y install autoconf 安装依赖 yum -y install autoconf 安装mysql rpm -iv ...

  9. Golang ---基准测试

    什么是基准测试 基准测试,是一种测试代码性能的方法,比如你有多种不同的方案,都可以解决问题,那么到底是那种方案性能更好呢?这时候基准测试就派上用场了. 基准测试主要是通过测试CPU和内存的效率问题,来 ...

  10. hibernate左连接查询时在easyUI的dataGrid中有些行取值为空的解决办法

    1 当使用left join左连连接,sql语句为 select t from SecondPage t left join t.rightNavbar n where 1=1 页面中出现了部分空行的 ...