用python创建线程池
来源:互联网 发布:js动态生成表格9*9 编辑:程序博客网 时间:2024/05/21 05:59
面向对象开发中,大家知道创建和销毁对象是很费时间的,因为创建一个对象要获取内存资源或者其它更多资源。无节制的创建和销毁线程是一种极大的浪费。那我们可不可以把执行完任务的线程不销毁而重复利用呢?仿佛就是把这些线程放进一个池子,一方面我们可以控制同时工作的线程数量,一方面也避免了创建和销毁产生的开销。
线程在官方文档没有明确说明,但是是在multiprocessing 中给出了定义
from multiprocessing.pool import ThreadPoolimport threading,timepool = ThreadPool(3);#定义一个线程池,线程池中有3个线程,等待调用def double(n): t = threading.currentThread() time.sleep(1) print('当前进程名:{},计算结果为{}'.format(t.name,n*2))pool.map(double,range(5));打印结果:
当前进程名:Thread-110,计算结果为2
当前进程名:Thread-112,计算结果为0
当前进程名:Thread-111,计算结果为4
当前进程名:Thread-110,计算结果为6
当前进程名:Thread-112,计算结果为8
下面我们自定义一个线程:
import threading, queue,timedef double(n): return 2*nclass MyThread(threading.Thread):#自定义一个进程类 def __init__(self,queue): super(MyThread,self).__init__()##先继续父类所有的属性 self._queue = queue# 用来存储傻 self.daemon = True# 进程必须是守护进程(进程完成后不会死掉) self.start()#进程是要启动状态 def run(self):#重定义run函数 while 1: f,args,kargs = self._queue.get()#f为函数,其它为参数 try: print('using {}'.format(self._name))#打印进程名 print(f(*args,**kargs)) except Exception as e: print(e) self._queue.task_done() ## 任务完成, class ThreadPool():##定义一俱个线程池 def __init__(self,size): self._queue =queue.Queue(size) for _ in range(size):#在进程池生成进程 MyThread(self._queue) def add_task(self,f,*args,**kargs): self._queue.put((f, args, kargs))#给进程执行任务 def wail_complete(self): self._queue.join()#阻碍进程到其它进程全部完成pool = ThreadPool(5)for i in range(10):#给线程池中加了10个任务 pool.add_task(double,i) time.sleep(0.5)pool.wail_complete()
执行结果:
using Thread-121
0
using Thread-122
2
using Thread-123
4
using Thread-124
6
using Thread-125
8
using Thread-121
10
using Thread-122
12
using Thread-123
14
using Thread-124
16
using Thread-125
18
结果分析 :十个任务,用的始终是进程池中的五个进程
阅读全文
0 0
- 用python创建线程池
- python 线程池创建
- python 线程池的创建
- python之创建线程
- python创建线程
- 【Python】批量创建线程
- Python 创建线程
- Python线程的创建
- python创建线程
- python基础-线程创建、线程池、进\线程异步回调(add_done_callback)、进\线程数据共享、ftp线程池
- 创建线程及线程池
- Python练习代码 -- 线程创建, 同步
- Python多线程之线程创建和终止
- Python线程的两种创建方法
- python多线程编程—创建线程
- Python 使用threading 模块创建线程
- python 创建多个线程并启动
- linux用C语言创建线程池
- cifar数据集基于Caffe的生成与测试
- 设计模式—中介者模式(二十一)
- win10局域网共享文件方法
- 那些年,我们喜欢的String
- Unity编程 欧拉角与万向节死锁(图文版)
- 用python创建线程池
- Leetcode 174. Dungeon Game
- Python中的sorted函数以及operator.itemgetter函数
- 题解——Leetcode 240. Search a 2D Matrix II 难度:Medium
- 数据库中的全表扫描和索引扫描
- 第三十九、四十天:Aurora的服务层ASL(AuroraServiceLayer)的数据传输框架分析学习
- Tomcat发布项目
- CodeForces
- lnmp忘记mysql密码怎么办?