参考文献

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. Vue实现勾选后向数组都添加

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  2. 配置eNSP和本地电脑上的网卡相连,从而直接从本地电脑连接设备

  3. Github Page+Bmob实现简单动态功能

    Github Page基于jekyll能够实现简单的静态网站,但是没有提供后端服务.目前国内外也有很多提供后台服务,特别是云服务.譬如国外有AWS,记得好像是注册免费使用一年:再如Heroku,支持N ...

  4. 学习笔记之09小练习题(js:从小到大输出三个任意数,查成绩,相亲题,查体重,一元二次方程求根)

    <script type="text/javascript"> /*第一题.输入三个整数,x,y,z,最终以从小到大的方式输出. 注意小点:1 字符串的拼接是用+,数学 ...

  5. IOS系统配置FFMEPG

    在FFMPEG的官网上可以找到标准的配置文档...http://ffmpeg.org/trac/ffmpeg/wiki/MacOSXCompilationGuide 在开始前确保安装了XCODE而且也 ...

  6. [Python] Codecombat 攻略 Sarven 沙漠 (1-43关)截止至30关

    首页:https://cn.codecombat.com/play语言:Python 第二界面:Sarven沙漠(43关)时间:4-11小时内容:算术运算,计数器,while循环,break(跳出循环 ...

  7. flask + wtform + google storage

    项目需要使用 flask 上传.下载文件到 google storage 上, 搜了一圈没有能直接结合 wtform 使用的插件,所以动手造了个轮子. 只实现了基本的上传,下载的功能,后续可能会完善预 ...

  8. cordova封装h5为app,cookie不可用解决方法

    //创建cookie function setCookie(c_name,value,expiredays) { var exdate=new Date(); exdate.setDate(exdat ...

  9. Stack by pointer

    前言:因为栈的很多操作是基于表的,所以这篇文章里的例程就不再大面积地写注释了,有不理解的地方可以翻看之前的链表笔记,或者直接写在评论区. 咳咳,说到这个栈,很多人乍听之下感觉很陌生.卧槽这是什么玩意. ...

  10. asp.net中使用jquery ajax保存富文本的问题

    前提:为了保证页面的不刷新行为,所以采用了html+jquery+handler的页面保存方式,通过ajax将富文本内容传递给一般处理程序进行操作. 一.问题:1.大文件无法上传? 2.传入handl ...