参考文献

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. Oracle的常用命令之备份和恢复数据库

    1 将数据库TES完全导出,用户名system 密码manager 导出到D:\daochu.dmp中 exp system/manager@TEST file=d:\daochu.dmp 2 将数据 ...

  2. EasyUI ComboTree无限层级异步加载示例

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="EasuUIDemoTree.a ...

  3. MySQL笔记 存储过程 游标 触发器

    第二十三章 使用存储过程 MySQL5 中添加了存储过程的支持. 大多数SQL语句都是针对一个或多个表的单条语句.并非所有的操作都怎么简单.经常会有一个完整的操作需要多条才能完成 存储过程简单来说,就 ...

  4. 【转载】jQuery手机移动端触屏日历日期选择

    文章转载自 科e互联 http://www.internetke.com/ 原文链接:http://www.internetke.com/effects/css3/2015/0120/1222.htm ...

  5. 自学 Python 3 最好的 入门 书籍 推荐(附 免费 在线阅读 下载链接)

    请大家根据自己的实际情况对号入座,挑选适合自己的 Python 入门书籍: 完全没有任何编程基础:01 号书 少量编程基础,不求全,只希望能以最快的速度入门:02 号书 少量编程基础,有一定的英文阅读 ...

  6. JAVA基础知识总结:一

    一.软件开发的常识 1.什么是软件? 一系列按照特定顺序组织起来的计算机数据或者指令 常见的软件: 系统软件:Windows\Mac OS \Linux 应用软件:QQ,一系列的播放器(爱奇艺,乐视, ...

  7. h5实现照片墙效果

    <style> *{ margin: 0; padding: 0; } body{ background: url(images/bg.jpg); } #div1{ width: 100% ...

  8. LeetCode 1. Two Sum (两数之和)

    Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...

  9. C#的常见算法(面试)

    一.求以下表达式的值,写出您想到的一种或几种实现方法: 1-2+3-4+--+m //方法一,通过顺序规律写程序,同时也知道flag标志位的重要性. static int F1(int m) { ; ...

  10. Arduino上传数据至贝壳物联并与FPGA进行交互

    本篇实现Arduino与FPGA交互,当然也没有什么新的协议,还是基于串口通讯,现在学一个串口通信基本上可以驱动大多数模块了,而且和各种单片机无缝数据交互,Arduino由于其强大的库函数支持,在实现 ...