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密 ...
随机推荐
- 构建web应用之——maven创建以及SpringMVC配置
构建web应用第一步需要创建以及配置maven项目管理,同时配置启动SpringMVC,这里推荐参考CSDN的一篇文章链接:https://blog.csdn.net/weixin_42222334/ ...
- 小白的python之路10/31&11/1文件操作系统
文件操作系统的介绍 ext4的superblock块是超级快,innode 块是专门存放文件信息的, block count将硬盘做成block块,对操作系统而言写在block块上就可了,eg:文件1 ...
- Rhino学习教程——1.2
实战——创建个性化工具栏: 因为我们的制图习惯不同,所以可以吧自己常用的工具放在一起.我上次说的自定义界面就是这个和调整工具栏位置. 1.打开常用>设置 2.选择工具列>编辑>新增工 ...
- 电梯问题——致敬ACM
The Fair Nut and Elevator time limit per test 1 second memory limit per test 256 megabytes inp ...
- org.apache.commons.dbcp.DelegatingPreparedStatement.isClosed()Z和NewProxyPreparedStatement.isClosed()
原文转:https://blog.csdn.net/qq_37909508/article/details/83028536 报错: NewProxyPreparedStatement.isClose ...
- python之路-模块初识
# sys模块 import sys #print (sys.path) #打印环境变量 print (sys.arge) print (sys.arge[2]) # os模块 import os # ...
- vue组件的通信
组件的引入两种方式 动态组件 <p is="com-a"></p> 直接引入 <com-a></com-a> 插槽功能 父组件引入 ...
- Python随笔--Pickle
遇到问题:不能文件名为pickle.py
- dubbo-admin 配置运行
1.下载dubbo源码:http://dubbo.io/ 2.idea 导入maven项目 3.配置tomcat:http://localhost:8080/ 4.下载zookeeper:http:/ ...
- L1-060 心理阴影面积
这是一幅心理阴影面积图.我们都以为自己可以匀速前进(图中蓝色直线),而拖延症晚期的我们往往执行的是最后时刻的疯狂赶工(图中的红色折线).由红.蓝线围出的面积,就是我们在做作业时的心理阴影面积. 现给出 ...