python线程池实现多线程
参考文献
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线程池实现多线程的更多相关文章
- Python线程池与进程池
Python线程池与进程池 前言 前面我们已经将线程并发编程与进程并行编程全部摸了个透,其实我第一次学习他们的时候感觉非常困难甚至是吃力.因为概念实在是太多了,各种锁,数据共享同步,各种方法等等让人十 ...
- java笔记--使用线程池优化多线程编程
使用线程池优化多线程编程 认识线程池 在Java中,所有的对象都是需要通过new操作符来创建的,如果创建大量短生命周期的对象,将会使得整个程序的性能非常的低下.这种时候就需要用到了池的技术,比如数据库 ...
- 基于线程池的多线程售票demo2.0(原创)
继上回基于线程池的多线程售票demo,具体链接: http://www.cnblogs.com/xifenglou/p/8807323.html以上算是单机版的实现,特别使用了redis 实现分布式锁 ...
- 自定义高级版python线程池
基于简单版创建类对象过多,现自定义高级版python线程池,代码如下 #高级线程池 import queue import threading import time StopEvent = obje ...
- 对Python线程池
本文对Python线程池进行详细说明介绍,IDE选择及编码的解决方案进行了一番详细的描述,实为Python初学者必读的Python学习经验心得. AD: 干货来了,不要等!WOT2015 北京站演讲P ...
- Python 线程池(小节)
Python 线程池(小节) from concurrent.futures import ThreadPoolExecutor,ProcessPoolExecutor import os,time, ...
- python线程池ThreadPoolExecutor(上)(38)
在前面的文章中我们已经介绍了很多关于python线程相关的知识点,比如 线程互斥锁Lock / 线程事件Event / 线程条件变量Condition 等等,而今天给大家讲解的是 线程池ThreadP ...
- python线程池及其原理和使用
python线程池及其原理和使用 2019-05-29 17:05:20 whatday 阅读数 576 系统启动一个新线程的成本是比较高的,因为它涉及与操作系统的交互.在这种情形下,使用线程池可以很 ...
- python线程池示例
使用with方式创建线程池,任务执行完毕之后,会自动关闭资源 , 否则就需要手动关闭线程池资源 import threading, time from concurrent.futures impo ...
随机推荐
- 错误:Cannot set property 'innerHTML' of null
360浏览器代码编辑器里提示错误:Cannot set property 'innerHTML' of null 原因是代码执行时要调用的内容不存在
- 有趣的flash例子
仓鼠 <object type="application/x-shockwave-flash" data="http://cdn.abowman.com/widge ...
- C#微信公众号/订阅号开发 接口源码
using System; using System.Web; using System.IO; using System.Text; using System.Web.Security; using ...
- c#中list使用示例
protected void Page_Load(object sender, EventArgs e) { List<string> studentNames = new List< ...
- DataRow和DataRowView的区别
可以将DataView同数据库的视图类比,不过有点不同,数据库的视图可以跨表建立视图,DataView则只能对某一个DataTable建立视图. DataView一般通过DataTable.Defau ...
- uva242,Stamps and Envelope Size
这题紫薯上翻译错了 应该是:如果有多个并列,输出邮票种类最少的那个,如果还有并列,输出最大面值最小的那个 坑了我一个下午 dp[p][q]==1表示可以用不超过q张组成面额p 结合记忆化,p从1开始枚 ...
- UVa437,The Tower of Babylon
转:http://blog.csdn.net/wangtaoking1/article/details/7308275 题意为输入若干种立方体(每种若干个),然后将立方体堆成一个塔,要求接触的两个面下 ...
- javascript高性能写法
看到一篇不错的博文,如果想写出比较高性能的代码,可参看这个链接http://developer.51cto.com/art/200906/131335.htm
- Java调用C++类库--JNI
JNI是Java平台中的一个重要的功能,这里我把我做的Demo总结一下,分享一下,我会把每个步骤尽量的详细的展现出来. 这里我就不讲解JNI的原理了,google,百度一下,到处都是 好了,直接来讲步 ...
- A+B problems
这几道习题大概是ACM输入输出格式的普及 P1:---------------------------------------------------------------------------- ...