[心得]利用python并发提速上线测试效率
来源:互联网 发布:java cas干什么用的 编辑:程序博客网 时间:2024/06/08 04:38
背景
在我们的工作中,分布式的生产环境要求我们提高测试效率。原先的上线job串行执行,严重制约了我们的上线效率。
我们从两个层面来解决问题:jenkins job层面,设置里面勾选Execute concurrent builds if necessary来实现多job并行。脚本层面,引入python并发来解决脚本内串行的问题。
取舍
关于多线程还是多进程的取舍。
如果是IO密集型,线程和进程都可以,相对而言,线程稍复杂。
如果是cpu密集型,那么多进程更合理。
线程
线程模型如下:
做一个线程池,总数为cpu数加1,每一个子类无限循环。
# coding=utf-8from Queue import Queuefrom threading import Threadfrom single import *class ProcessWorker(Thread): def __init__(self, queue): Thread.__init__(self) self.queue = queue def run(self): while True: # Get the work from the queue num = self.queue.get() processNum(num) self.queue.task_done()def main(): ts = time() nums = getNums(4) # Create a queue to communicate with the worker threads queue = Queue() # Create 4 worker threads for x in range(4): worker = ProcessWorker(queue) # Setting daemon to True will let the main thread exit even though the workers are blocking worker.daemon = True worker.start() # Put the tasks into the queue for num in nums: queue.put(num) # Causes the main thread to wait for the queue to finish processing all the tasks queue.join() print("cost time is: {:.2f}s".format(time() - ts))if __name__ == "__main__": main()
进程
每当线程中的一个准备工作时,进程可以不断转换线程。使用Python或其他有GIL的解释型语言中的线程模块实际上会降低性能。如果你的代码执行的是CPU密集型的任务,例如解压gzip文件,使用线程模块将会导致执行时间变长。对于CPU密集型任务和真正的并行执行,我们可以使用多进程(multiprocessing)模块。
官方的Python实现——CPython——带有GIL.
为了使用多进程,我们得建立一个多进程池。通过它提供的map方法,我们把URL列表传给池。
# coding=utf-8from functools import partialfrom multiprocessing.pool import Poolfrom single import *from time import timedef main(): ts = time() nums = getNums(4) p = Pool(4) p.map(processNum, nums) print("cost time is: {:.2f}s".format(time() - ts))if __name__ == "__main__": main()
关于并发和并行的理解
并发是指,程序在运行的过程中存在多于一个的执行上下文。这些执行上下文一般对应着不同的调用栈。
在单处理器上,并发程序虽然有多个上下文运行环境,但某一个时刻只有一个任务在运行。
但在多处理器上,因为有了多个执行单元,就可以同时有数个任务在跑。
这种物理上同一时刻有多个任务同时运行的方式就是并行。
和并发相比,并行更加强调多个任务同时在运行。
而且并行还有一个层次问题,比如是指令间的并行还是任务间的并行。
0 0
- [心得]利用python并发提速上线测试效率
- python 串口效率测试
- Python效率测试计时器()
- python效率测试
- python测试心得
- 利用交叉测试提升软件测试效率
- python并发测试脚本
- python 并发测试
- surefire单元测试 并发 提速
- python多进程多核利用心得体验
- 利用多线程处理问题心得----处理并发程序
- 利用python做压力测试
- 利用python做ui测试
- 利用钩子函数测试某段代码的执行效率
- 试试利用eoLinker进行环境管理提高测试效率
- 测试python的并行模块Parallel Python的效率
- C++程序提速----测试阶段
- 利用c模块代码,提升python代码执行效率
- 高级安全windows防火墙管理单元无法加载解决方法
- i+++i的解释及i+++++i的语法错误
- 面向对象_匿名内部类的格式和理解
- Python学习-用户交互raw_input()
- HDOJ 5724 Chess (SG)
- [心得]利用python并发提速上线测试效率
- Codeforces 730G Car Repair Shop
- python通过API查阅热点新闻
- 2014~2016百度之星hdu题目
- 和《创时代》
- Linux学习--第四天--find、locate、帮助命令、grep、who、w、压缩命令、网络命令、mount
- Java——第二章(变量)
- 并查集的find和join函数实现
- 泛型是个巨坑