对于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密集型任务、计算密集型任务,以及多线程、多进程的更多相关文章

  1. IO密集型和计算密集型

    我们常说的多任务或者单任务分为两种: IO密集型的任务  计算密集型的任务   IO密集型的任务或:有阻塞的状态,就是不一直会运行CPU(中间就一个等待状态,就告诉CPU 等待状态,这个就叫IO密集型 ...

  2. 流动python - 写port扫描仪和各种并发尝试(多线程/多进程/gevent/futures)

    port扫描仪的原理非常easy.没有什么比操作更socket,能够connect它认为,port打开. import socket def scan(port): s = socket.socket ...

  3. CPU-bound(计算密集型) 和I/O bound(I/O密集型) 区别 与应用

    I/O密集型 (CPU-bound) I/O bound 指的是系统的CPU效能相对硬盘/内存的效能要好很多,此时,系统运作,大部分的状况是 CPU 在等 I/O (硬盘/内存) 的读/写,此时 CP ...

  4. [转]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效能相对硬盘/ ...

  5. 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效能相对硬盘 ...

  6. 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效能相对硬盘/内存的 ...

  7. Python多线程多进程那些事儿看这篇就够了~~

    自己以前也写过多线程,发现都是零零碎碎,这篇写写详细点,填一下GIL和Python多线程多进程的坑~ 总结下GIL的坑和python多线程多进程分别应用场景(IO密集.计算密集)以及具体实现的代码模块 ...

  8. [Python]IO密集型任务 VS 计算密集型任务

    所谓IO密集型任务,是指磁盘IO.网络IO占主要的任务,计算量很小.比如请求网页.读写文件等.当然我们在Python中可以利用sleep达到IO密集型任务的目的. 所谓计算密集型任务,是指CPU计算占 ...

  9. Python进阶----GIL锁,验证Cpython效率(单核,多核(计算密集型,IO密集型)),线程池,进程池

    day35 一丶GIL锁 什么是GIL锁:    存在Cpython解释器,全名:全局解释器锁.(解释器级别的锁) ​   GIL是一把互斥锁,将并发运行变成串行. ​   在同一个进程下开启的多个线 ...

  10. Python并发编程05 /死锁现象、递归锁、信号量、GIL锁、计算密集型/IO密集型效率验证、进程池/线程池

    Python并发编程05 /死锁现象.递归锁.信号量.GIL锁.计算密集型/IO密集型效率验证.进程池/线程池 目录 Python并发编程05 /死锁现象.递归锁.信号量.GIL锁.计算密集型/IO密 ...

随机推荐

  1. 版本控制commit和update过程

    很早就使用了git.后来还管了一个VSS,但长时间以来git和VSS基本都当ftp使用,顶多知道其有回退旧版本的功能,但对“版本控制”这个词一直以来都没领会其内含. 比如我一直担心两个问题,一是拉取下 ...

  2. select top 1 和select top 1 with ties * from SC 的区别

    select top1 : * FROM SC ORDER BY score DESC 运行结果如下: sc表数据查询select top 1 S# C# Score 001 03 120 WITH ...

  3. var that = this 小坑记

    在js编码过程中,经常会使用如上的语句来规避拿不到变量的问题. 比如: queryData:function () { var that=this; var param={}; for(var key ...

  4. Lapack求解线性方程组

    可参见这两个页面: 1. http://www.culatools.com/dense/lapack/ 2. http://www.netlib.org/lapack/lug/node1.html 根 ...

  5. USM-V1.0

    ADSP-BF512 :Low Power Blackfin with Consumer Devices Connectivity The ADSP-BF512 is the low cost ent ...

  6. sql:按年、月、日钻取时间

    #按月排SELECT count(EN_NAME), DATE_FORMAT( CREATE_DATE, "%Y-%m" )FROM financeWHERE DATE_FORMA ...

  7. 不使用接口的 limit 控制分页的容量

    1.html中v-for 此时的v-for对象并不是在后台获取的数组list,而是计算属性的函数名pageList <div v-for="item in pageList" ...

  8. 20145338 《网络对抗》逆向及Bof基础实验

    逆向及Bof基础实验 实践目标 ·本次实践的对象是一个名为pwn1的linux可执行文件. ·该程序正常执行流程是:main调用foo函数,foo函数会简单回显任何用户输入的字符串. ·该程序同时包含 ...

  9. SQL中的with check option(转)

    student表: 95001 李勇 男 20 CS 95002 刘晨 女 21 IS 95003 王敏 女 18 MA 95004 张力 男 19 IS 建立视图IS_STUDENT显示“IS”系所 ...

  10. Filebeat占用内存和CPU过高问题排查

    经反馈,新部署的服务器上filebeat占用的cpu过高,且内存只增不减. 而据我了解filebeat非常轻量级,正常情况下占用的资源几乎都能忽略不计,所以怀疑是filebeat本身出了问题. 第一时 ...