测试Python多线程与多进程
2018-01-29 11:22
573 查看
#!/usr/bin/python # -*- coding: UTF-8 -*- import time import os from threading import Thread from multiprocessing import Process def count(x, y): # 使程序完成大量计算 for i in xrange(100000): x += x y += y def write(filename): f = open(filename, "w") for i in xrange(100000): f.write("testwrite\n") f.close() def read(filename): f = open(filename, "r") lines = f.readlines() f.close() def io(filename): write(filename) read(filename) def testSingleProcess(): # CPU密集操作 t = time.time() for i in xrange(10): count(1, 1) print("SingleProcess cpu", time.time() - t) # IO密集操作 t = time.time() for i in xrange(10): io("test%d.txt"%i) print("SingleProcess IO", time.time() - t) def testMultithreading(): counts = [] t = time.time() for i in xrange(10): thread = Thread(target=count, args=(1,1)) counts.append(thread) thread.start() e = counts.__len__() while True: for th in counts: if not th.is_alive(): e -= 1 if e <= 0: break print("Multithreading cpu",time.time() - t) ios = [] t = time.time() for i in xrange(10): thread = Thread(target=io, args=("test%d.txt"%i,)) ios.append(thread) thread.start() e = ios.__len__() while True: for th in ios: if not th.is_alive(): e -= 1 if e <= 0: break print("Multithreading IO",time.time() - t) def testMultiprocess(): counts = [] t = time.time() for i in xrange(10): process = Process(target=count, args=(1,1)) counts.append(process) process.start() e = counts.__len__() while True: for th in counts: if not th.is_alive(): e -= 1 if e <= 0: break print("Multiprocess cpu", time.time() - t) ios = [] t = time.time() for i in xrange(10): process = Process(target=io, args=("test%d.txt"%i,)) ios.append(process) process.start() e = ios.__len__() while True: for th in ios: if not th.is_alive(): e -= 1 if e <= 0: break print("Multiprocess IO", time.time() - t) def clearTmpFile(): for i in xrange(10): os.remove("test%d.txt"%i) if __name__ == "__main__": testSingleProcess() time.sleep(2) clearTmpFile() time.sleep(2) testMultithreading() time.sleep(2) clearTmpFile() time.sleep(2) testMultiprocess() time.sleep(2) clearTmpFile()
测试时的cpu运行 情况如下:
运行结果:
('SingleProcess cpu', 41.20599985122681)
('SingleProcess IO', 0.3340001106262207)
('Multithreading cpu', 41.062999963760376)
('Multithreading IO', 4.700000047683716)
('Multiprocess cpu', 11.5239999294281)
('Multiprocess IO', 0.29200005531311035)
可以看出,只有多进程下,才能利用多核的优势,将大量计算快速执行完毕
原因是:
Python是运行在解释器中的语言,有一个全局锁(GIL),在使用多进程(Thread)的情况下,不能发挥多核的优势。
而使用多进程(Multiprocess),则可以发挥多核的优势真正地提高效率。
另外,Python还有基于协程的网络库gevent(github)
协程不同于线程的地方在于协程不是操作系统进行切换,而是由编码进行切换,即由程序员控制的,这样就没有了线程的所谓安全问题。
相关文章推荐
- Python 多线程多进程10.4.1 应用于自动化测试项目
- 也说性能测试,顺便说python的多进程+多线程、协程
- python入门三(多线程,多进程)
- 【python】多进程+多线程 制作智联招聘爬虫 写入CSV+mongodb
- Python多线程与多进程
- Python多线程与多进程
- python多进程、多线程服务器和客户端的简单实现
- python并发编程之多进程、多线程、异步和协程详解
- Python 多进程 多线程数据共享
- 【转】【Python】Python多进程与多线程
- 深入浅析python中的多进程、多线程、协程
- 深入浅析python中的多进程、多线程、协程
- python学习笔记(十三): 多线程多进程
- Python多线程与多进程
- python创建多进程/多线程
- Python之多进程和多线程
- python【第九篇】多线程、多进程
- Python 多进程 多线程 协程 I/O多路复用
- python 多进程开发与多线程开发
- 【多进程与多线程】为什么在Python里推荐使用多进程而不是多线程?