Python中线程和函数的区别--实例演示
来源:互联网 发布:厄米特矩阵的性质 编辑:程序博客网 时间:2024/05/16 18:35
假设我们要检查本地网络中哪些IP地址是可达的哪些是不可达的,我们可以用循环来实现,也可以用线程来实现。
不用线程的实现方式:
#!/usr/bin/pythonimport os, re, threading, commands, timestatus = ["no respones","alive, but 50% package loss","alive","shouldn't occur"]received_packages = re.compile(r"(\d) received")for suffix in range(20,29): ip = "10.170.9."+str(suffix) ping_out = os.popen("ping -c 2 " + ip ,"r") while True: line = ping_out.read() if not line: break n_received = re.findall(received_packages, line) if n_received: print "Status from ", ip, " is ", status[int( n_received[0])],time.ctime(time.time())
执行结果:
Status from 10.170.9.20 is alive Fri Apr 17 13:57:03 2015
Status from 10.170.9.21 is no respones Fri Apr 17 13:57:06 2015
Status from 10.170.9.22 is no respones Fri Apr 17 13:57:09 2015
Status from 10.170.9.23 is no respones Fri Apr 17 13:57:12 2015
Status from 10.170.9.24 is no respones Fri Apr 17 13:57:15 2015
Status from 10.170.9.25 is alive Fri Apr 17 13:57:16 2015
Status from 10.170.9.26 is alive Fri Apr 17 13:57:17 2015
Status from 10.170.9.27 is alive Fri Apr 17 13:57:18 2015
Status from 10.170.9.28 is no respones Fri Apr 17 13:57:22 2015
用线程的实现方式:
#!/usr/bin/pythonimport os, re, threading, commands, timeclass ip_check(threading.Thread): def __init__(self, ip): threading.Thread.__init__(self) self.ip = ip self.__successful_pings = -1 def run(self): ping_out = os.popen("ping -c 2 " + self.ip ,"r") while True: line = ping_out.read() if not line: break n_received = re.findall(received_packages, line) if n_received: self.__successful_pings = int( n_received[0]) def status(self): if self.__successful_pings == 0: return "no respones" elif self.__successful_pings == 1: return "alive, but 50% package loss" elif self.__successful_pings == 2: return "alive" else: return "shouldn't occur"received_packages = re.compile(r"(\d) received")check_results = []for suffix in range(20,29): ip = "10.170.9."+str(suffix) current = ip_check(ip) check_results.append(current) current.start()for el in check_results: el.join() print "Status from ", el.ip, " is ", el.status(),time.ctime(time.time())执行结果:
Status from 10.170.9.20 is alive Fri Apr 17 13:57:39 2015
Status from 10.170.9.21 is no respones Fri Apr 17 13:57:41 2015
Status from 10.170.9.22 is no respones Fri Apr 17 13:57:41 2015
Status from 10.170.9.23 is no respones Fri Apr 17 13:57:41 2015
Status from 10.170.9.24 is no respones Fri Apr 17 13:57:41 2015
Status from 10.170.9.25 is alive Fri Apr 17 13:57:41 2015
Status from 10.170.9.26 is alive Fri Apr 17 13:57:41 2015
Status from 10.170.9.27 is alive Fri Apr 17 13:57:41 2015
Status from 10.170.9.28 is no respones Fri Apr 17 13:57:41 2015
无线程和有线程的结果分析:
有线程的耗时是: 2秒
无线程的耗时是:19秒
线程的方式明显比无线程的快将近10倍。如果在要处理的数据量更大的情况下,线程的处理方式的优势更明显。
- Python中线程和函数的区别--实例演示
- java中Comparable 和 Comparator 区别及实例演示
- wait和sleep的区别 以及 实例演示
- (转)wait和sleep的区别 以及 实例演示
- Python中进程和线程的总体区别
- Python中进程和线程的区别详解
- Python中类成员和实例成员的区别
- python中类属性和实例属性的区别
- python中range函数和xrange函数的区别
- python中sort()函数和sorted()函数的区别
- GTK中创建线程函数g_thread_new和g_thread_create的区别
- Python中进程和线程总体区别
- 线程的等待/唤醒机制实例演示
- 实例:Python中*args和**kwargs区别
- javascript和python函数中变量作用域的区别
- python函数中*args和**kwargs的区别
- python和matlab中reshape函数的区别
- python中map()和reduce()函数的区别
- odsocket 简单使用
- js 使用正则 将字符串中的URL加上href链接 在页面点击
- 代码生成Excel导入模版
- OC block变量的理解
- Magento性能优化—开Gzip提高网页下载速度
- Python中线程和函数的区别--实例演示
- C语言输出方式
- 指针面试1
- 内核bug.h以及GCC内联汇编的学习
- 又见数塔
- NYOJ 304 节能 (区间dp)
- Java中的泛型方法
- Apache与Tomcat的关系?
- cannot open file "C:\Program.obj"