Python IO密集型任务、计算密集型任务,以及多线程、多进程
对于IO密集型任务:
- 直接执行用时:10.0333秒
- 多线程执行用时:4.0156秒
- 多进程执行用时:5.0182秒
说明多线程适合IO密集型任务。
对于计算密集型任务
- 直接执行用时:10.0273秒
- 多线程执行用时:13.247秒
- 多进程执行用时:6.8377秒
说明多进程适合计算密集型任务。
#coding=utf-8
import sys
import multiprocessing
import time
import threading # 定义全局变量Queue
g_queue = multiprocessing.Queue() def init_queue():
print("init g_queue start")
while not g_queue.empty():
g_queue.get()
for _index in range(10):
g_queue.put(_index)
print("init g_queue end")
return # 定义一个IO密集型任务:利用time.sleep()
def task_io(task_id):
print("IOTask[%s] start" % task_id)
while not g_queue.empty():
time.sleep(1)
try:
data = g_queue.get(block=True, timeout=1)
print("IOTask[%s] get data: %s" % (task_id, data))
except Exception as excep:
print("IOTask[%s] error: %s" % (task_id, str(excep)))
print("IOTask[%s] end" % task_id)
return g_search_list = list(range(10000))
# 定义一个计算密集型任务:利用一些复杂加减乘除、列表查找等
def task_cpu(task_id):
print("CPUTask[%s] start" % task_id)
while not g_queue.empty():
count = 0
for i in range(10000):
count += pow(3*2, 3*2) if i in g_search_list else 0
try:
data = g_queue.get(block=True, timeout=1)
print("CPUTask[%s] get data: %s" % (task_id, data))
except Exception as excep:
print("CPUTask[%s] error: %s" % (task_id, str(excep)))
print("CPUTask[%s] end" % task_id)
return task_id if __name__ == '__main__':
print("cpu count:", multiprocessing.cpu_count(), "\n") print(u"========== 直接执行IO密集型任务 ==========")
init_queue()
time_0 = time.time()
task_io(0)
print(u"结束:", time.time() - time_0, "\n") print("========== 多线程执行IO密集型任务 ==========")
init_queue()
time_0 = time.time()
thread_list = [threading.Thread(target=task_io, args=(i,)) for i in range(10)]
for t in thread_list:
t.start()
for t in thread_list:
if t.is_alive():
t.join()
print("结束:", time.time() - time_0, "\n") print("========== 多进程执行IO密集型任务 ==========")
init_queue()
time_0 = time.time()
process_list = [multiprocessing.Process(target=task_io, args=(i,)) for i in range(multiprocessing.cpu_count())]
for p in process_list:
p.start()
for p in process_list:
if p.is_alive():
p.join()
print("结束:", time.time() - time_0, "\n") print("========== 直接执行CPU密集型任务 ==========")
init_queue()
time_0 = time.time()
task_cpu(0)
print("结束:", time.time() - time_0, "\n") print("========== 多线程执行CPU密集型任务 ==========")
init_queue()
time_0 = time.time()
thread_list = [threading.Thread(target=task_cpu, args=(i,)) for i in range(10)]
for t in thread_list:
t.start()
for t in thread_list:
if t.is_alive():
t.join()
print("结束:", time.time() - time_0, "\n") print("========== 多进程执行cpu密集型任务 ==========")
init_queue()
time_0 = time.time()
process_list = [multiprocessing.Process(target=task_cpu, args=(i,)) for i in range(multiprocessing.cpu_count())]
for p in process_list:
p.start()
for p in process_list:
if p.is_alive():
p.join()
print("结束:", time.time() - time_0, "\n")
参考:https://zhuanlan.zhihu.com/p/24283040
Python IO密集型任务、计算密集型任务,以及多线程、多进程的更多相关文章
- IO密集型和计算密集型
我们常说的多任务或者单任务分为两种: IO密集型的任务 计算密集型的任务 IO密集型的任务或:有阻塞的状态,就是不一直会运行CPU(中间就一个等待状态,就告诉CPU 等待状态,这个就叫IO密集型 ...
- 流动python - 写port扫描仪和各种并发尝试(多线程/多进程/gevent/futures)
port扫描仪的原理非常easy.没有什么比操作更socket,能够connect它认为,port打开. import socket def scan(port): s = socket.socket ...
- CPU-bound(计算密集型) 和I/O bound(I/O密集型) 区别 与应用
I/O密集型 (CPU-bound) I/O bound 指的是系统的CPU效能相对硬盘/内存的效能要好很多,此时,系统运作,大部分的状况是 CPU 在等 I/O (硬盘/内存) 的读/写,此时 CP ...
- [转]CPU-bound(计算密集型) 和I/O bound(I/O密集型)
转自:http://blog.csdn.net/q_l_s/article/details/51538039 I/O密集型 (CPU-bound) I/O bound 指的是系统的CPU效能相对硬盘/ ...
- PU-bound(计算密集型) 和I/O bound(I/O密集型)
转载:https://blog.csdn.net/q_l_s/article/details/51538039 I/O密集型 (CPU-bound) I/O bound 指的是系统的CPU效能相对硬盘 ...
- CPU-bound(计算密集型) 和I/O bound(I/O密集型)/数据密集型
https://blog.csdn.net/q_l_s/article/details/51538039 I/O密集型 (CPU-bound)I/O bound 指的是系统的CPU效能相对硬盘/内存的 ...
- Python多线程多进程那些事儿看这篇就够了~~
自己以前也写过多线程,发现都是零零碎碎,这篇写写详细点,填一下GIL和Python多线程多进程的坑~ 总结下GIL的坑和python多线程多进程分别应用场景(IO密集.计算密集)以及具体实现的代码模块 ...
- [Python]IO密集型任务 VS 计算密集型任务
所谓IO密集型任务,是指磁盘IO.网络IO占主要的任务,计算量很小.比如请求网页.读写文件等.当然我们在Python中可以利用sleep达到IO密集型任务的目的. 所谓计算密集型任务,是指CPU计算占 ...
- Python进阶----GIL锁,验证Cpython效率(单核,多核(计算密集型,IO密集型)),线程池,进程池
day35 一丶GIL锁 什么是GIL锁: 存在Cpython解释器,全名:全局解释器锁.(解释器级别的锁) GIL是一把互斥锁,将并发运行变成串行. 在同一个进程下开启的多个线 ...
- Python并发编程05 /死锁现象、递归锁、信号量、GIL锁、计算密集型/IO密集型效率验证、进程池/线程池
Python并发编程05 /死锁现象.递归锁.信号量.GIL锁.计算密集型/IO密集型效率验证.进程池/线程池 目录 Python并发编程05 /死锁现象.递归锁.信号量.GIL锁.计算密集型/IO密 ...
随机推荐
- css 三角
http://peunzhang.github.io/demo/css_angle/index.html
- go ethereum源码分析 PartIV Transaction相关
核心数据结构: core.types.transaction.go type Transaction struct { data txdata // caches hash atomic.Value ...
- vue-cli axios跨域 + 反向代理模拟http请求host+referer
axios跨域 配置config->index.js中的proxyTable,内容如下: proxyTable: { // 跨域请求 '/api': { // 注意此处可设置为 '*' 代表不限 ...
- Windows安装nvm和node, 以及安装live-server
nvm下载源https://github.com/coreybutler/nvm-windows/releases 参考博客: 主要https://www.cnblogs.com/hamsterPP/ ...
- static(静态)关键字
class Person{String name; //成员变量,实例变量(实例中的变量) //共享数据出现在对象之前static String country="cn"; //对 ...
- @EnableHystrix
@EnableHystrix 启动熔断降级服务 @Component把普通的pojo类实例到spring容器中去,相当于配置文件中的<bean id="" class=&qu ...
- SoapUI并发模式
soapUI支持test suite, test case级别的并发,合理使用这个功能,可以让自动化脚本短时间内跑完,为release省下时间. 1. 如何开启并发模式 图示,click projec ...
- DevExpress v18.2新版亮点——Reporting篇(一)
行业领先的.NET界面控件——DevExpress v18.2日前正式发布,本站将以连载的形式为大家介绍新版本新功能.本文将介绍了DevExpress Reporting v18.2 的新功能,新版3 ...
- DevExpress v18.2新版亮点——DevExtreme篇(二)
行业领先的.NET界面控件2018年第二次重大更新——DevExpress v18.2日前正式发布,本站将以连载的形式为大家介绍新版本新功能.本文将介绍了DevExtreme Complete Sub ...
- Tkinter模块:Grid几何管理器
Tkinter模块是Python的标准库模块之一,也是使用Python语言进行图形化用户界面(GUI)开发的基础. 本文介绍一下Tkinter模块的Grid几何管理器. 使用VB.MFC进行GUI开发 ...