参考文献

http://www.open-open.com/news/view/1c0179b

http://blog.jobbole.com/52060/

按照这个博客,实现获取多台服务器的空间使用情况

代码如下:

#!/usr/bin/env python2.7
#-*- coding:utf-8 -*- from multiprocessing.dummy import Pool as ThreadPool
import subprocess
import time results = []
ip_list = [ip.strip() for ip in open('./ssd','r')] def getsize(host):
now = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
print host,now
command = "df -h"
sys_cmd = """ssh -n -f -i /usr/home/guosong/.ssh/id_rsa -p 26387 -o StrictHostKeyChecking=no -o ConnectTimeout=2 root@'%s' \"%s\" """ %(host,command) pipe = subprocess.Popen(sys_cmd,stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True,stdin=subprocess.PIPE)
stdout,stderr = pipe.communicate()
#print stdout results.append(stdout) pool = ThreadPool(4) pool.map(getsize,ip_list)
pool.close() pool.join()                               

效率测试:

获取所有端口对应的MySQL版本信息,使用循环和多线程对比

#!/usr/bin/env python2.7
#-*- coding:utf-8 -*- import urllib2
from multiprocessing.dummy import Pool as ThreadPool
import time result_list = []
def get_instance(port):
url = "http://api.xxxr.sina.com.cn/autosystem/get_iplist?token=xxx&username=xxx&port=%d" % port
reps = urllib2.urlopen(url) result = reps.read() result_list.append(result) if __name__ == '__main__':
port_list = [int(port.strip()) for port in open('./ports','r')] '''for port in port_list:
get_instance(port)''' pool = ThreadPool(10) pool.map(get_instance,port_list) pool.close() pool.join() print len(result_list)

时间:

[root@typhoeus79 mysql_version]# time ./get_verion.py #多线程方式
real 0m2.298s
user 0m0.353s
sys 0m0.114s [root@typhoeus79 mysql_version]# time ./get_verion.py #循环方式
real 0m15.341s
user 0m0.356s
sys 0m0.088s

效率有明显提升

map传入多个元素

#!/usr/bin/env python2.7
# -*-coding:utf8 -*-
from multiprocessing.dummy import Pool as ThreadPool def add(meta):
a = meta[0]
b = meta[1] print meta
return a + b if __name__ == '__main__':
pool = ThreadPool(4) alist = [1, 2, 3]
blist = [4, 5, 6] pool.map(add, zip(alist, blist))
pool.close()
pool.join()

  

使用zip方式

python线程池实现多线程的更多相关文章

  1. Python线程池与进程池

    Python线程池与进程池 前言 前面我们已经将线程并发编程与进程并行编程全部摸了个透,其实我第一次学习他们的时候感觉非常困难甚至是吃力.因为概念实在是太多了,各种锁,数据共享同步,各种方法等等让人十 ...

  2. java笔记--使用线程池优化多线程编程

    使用线程池优化多线程编程 认识线程池 在Java中,所有的对象都是需要通过new操作符来创建的,如果创建大量短生命周期的对象,将会使得整个程序的性能非常的低下.这种时候就需要用到了池的技术,比如数据库 ...

  3. 基于线程池的多线程售票demo2.0(原创)

    继上回基于线程池的多线程售票demo,具体链接: http://www.cnblogs.com/xifenglou/p/8807323.html以上算是单机版的实现,特别使用了redis 实现分布式锁 ...

  4. 自定义高级版python线程池

    基于简单版创建类对象过多,现自定义高级版python线程池,代码如下 #高级线程池 import queue import threading import time StopEvent = obje ...

  5. 对Python线程池

    本文对Python线程池进行详细说明介绍,IDE选择及编码的解决方案进行了一番详细的描述,实为Python初学者必读的Python学习经验心得. AD: 干货来了,不要等!WOT2015 北京站演讲P ...

  6. Python 线程池(小节)

    Python 线程池(小节) from concurrent.futures import ThreadPoolExecutor,ProcessPoolExecutor import os,time, ...

  7. python线程池ThreadPoolExecutor(上)(38)

    在前面的文章中我们已经介绍了很多关于python线程相关的知识点,比如 线程互斥锁Lock / 线程事件Event / 线程条件变量Condition 等等,而今天给大家讲解的是 线程池ThreadP ...

  8. python线程池及其原理和使用

    python线程池及其原理和使用 2019-05-29 17:05:20 whatday 阅读数 576 系统启动一个新线程的成本是比较高的,因为它涉及与操作系统的交互.在这种情形下,使用线程池可以很 ...

  9. python线程池示例

    使用with方式创建线程池,任务执行完毕之后,会自动关闭资源 , 否则就需要手动关闭线程池资源  import threading, time from concurrent.futures impo ...

随机推荐

  1. css3制作网页中常见的小箭头

    /* css3三角形(向上 ▲) */ div.arrow-up { width:0px; height:0px; border-left:5px solid transparent;  /* 右透明 ...

  2. zabbix基本操作

    zabbix基本操作 ---- 2016年终总结 二 包括的内容: 添加主机 查看监控数据 添加监控项 创建触发器 创建模版 添加报警 添加媒介 添加主机 进入页面 点击Configuration(大 ...

  3. ASP.NET Core 处理 404 Not Found

    问题 在没有修改任何配置的情况下,这是用户使用 Chrome 访问不存在的URL时会看到的内容: 幸运的是,处理错误状态代码非常简单,我们将在下面介绍三种技术. 解决方案 在以前的ASP.NET MV ...

  4. Excel导出插件

    前言 一个游戏通常需要10多个Excel表格或者更多来配置,一般会通过导出csv格式读取配置. 本文提供导出Excel直接生成c#文件,对应数据直接生成结构体和数组,方便开发排错和简化重复写每个表格的 ...

  5. Python学习笔记(五)

    Python学习笔记(五): 文件操作 另一种文件打开方式-with 作业-三级菜单高大上版 1. 知识点 能调用方法的一定是对象 涉及文件的三个过程:打开-操作-关闭 python3中一个汉字就是一 ...

  6. eclipse环境下,java操作MySQL的简单演示

    首先先通过power shell 进入MySQL 查看现在数据库的状态(博主是win10系统) 右键开始,选择Windows powershell ,输入MySQL -u用户名 -p密码 选择数据库( ...

  7. Struts2 06--系统拦截器防止数据重复提交

    一.拦截器简要概述 拦截器,在AOP(Aspect-Oriented Programming)中用于在某个方法或字段被访问之前,进行拦截然后在之前或之后加入某些操作.拦截是AOP的一种实现策略. 在W ...

  8. 【转】认识物理I/O构件- 主机I/O总线

    在数据离开系统内存总线后,它通常传输到另一条总线,即主机I/O总线.在今天的产品中,最常见的主机I/O总线是PCI总线,但也存在着几种其他的总线,如S -总线,EIS A总线及VME总线.主机I/O总 ...

  9. Centos7安装后出现please make your choice from '1' to e 解决方式

    [输入"1",按Enter键   输入"2",按Enter键    输入"q",按Enter键    输入"yes",按 ...

  10. 80端口被系统服务【kernel&System】占用解决方案

    netstat -ano | findstr port    //查看端口占用情况 tasklist | findstr port   //查看端口被占用的具体服务名 运行net stop http ...