Python中多线程和多处理的分析
发表于:2026-11-01 作者:千家信息网编辑
千家信息网最后更新 2026年11月01日,本篇内容主要讲解"Python中多线程和多处理的分析",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"Python中多线程和多处理的分析"吧!多线程简单地说,
千家信息网最后更新 2026年11月01日Python中多线程和多处理的分析
本篇内容主要讲解"Python中多线程和多处理的分析",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"Python中多线程和多处理的分析"吧!
多线程
简单地说,线程允许您并行地运行程序。花费大量时间等待外部事件的任务通常适合线程化。它们也称为I/O Bound任务例如从文件中读写,网络操作或使用API在线下载。让我们来看一个示例,它展示了使用线程的好处。
1. 没有线程
在本例中,我们希望通过顺序运行程序来查看从Unsplash API下载15张图像需要多长时间:
import requests import time img_urls = [ 'https://images.unsplash.com/photo-1516117172878-fd2c41f4a759', 'https://images.unsplash.com/photo-1532009324734-20a7a5813719', 'https://images.unsplash.com/photo-1524429656589-6633a470097c', 'https://images.unsplash.com/photo-1530224264768-7ff8c1789d79', 'https://images.unsplash.com/photo-1564135624576-c5c88640f235', 'https://images.unsplash.com/photo-1541698444083-023c97d3f4b6', 'https://images.unsplash.com/photo-1522364723953-452d3431c267', 'https://images.unsplash.com/photo-1513938709626-033611b8cc03', 'https://images.unsplash.com/photo-1507143550189-fed454f93097', 'https://images.unsplash.com/photo-1493976040374-85c8e12f0c0e', 'https://images.unsplash.com/photo-1504198453319-5ce911bafcde', 'https://images.unsplash.com/photo-1530122037265-a5f1f91d3b99', 'https://images.unsplash.com/photo-1516972810927-80185027ca84', 'https://images.unsplash.com/photo-1550439062-609e1531270e', 'https://images.unsplash.com/photo-1549692520-acc6669e2f0c' ] start = time.perf_counter() #start timer for img_url in img_urls: img_name = img_url.split('/')[3] #get image name from url img_bytes = requests.get(img_url).content with open(img_name, 'wb') as img_file: img_file.write(img_bytes) #save image to disk finish = time.perf_counter() #end timer print(f"Finished in {round(finish-start,2)} seconds") #results Finished in 23.101926751 seconds一共用时23秒。
2. 多线程
让我们看看Pyhton中的线程模块如何显著地改进我们的程序执行:
import time from concurrent.futures import ThreadPoolExecutor def download_images(url): img_name = img_url.split('/')[3] img_bytes = requests.get(img_url).content with open(img_name, 'wb') as img_file: img_file.write(img_bytes) print(f"{img_name} was downloaded") start = time.perf_counter() #start timer with ThreadPoolExecutor() as executor: results = executor.map(download_images,img_urls) #this is Similar to map(func, *iterables) finish = time.perf_counter() #end timer print(f"Finished in {round(finish-start,2)} seconds") #results Finished in 5.544147536 seconds我们可以看到,与不使用线程代码相比,使用线程代码可以显著提高速度。从23秒到5秒。
对于本例,请注意在创建线程时存在开销,因此将线程用于多个API调用是有意义的,而不仅仅是单个调用。
此外,对于密集的计算,如数据处理,图像处理多处理比线程执行得更好。
到此,相信大家对"Python中多线程和多处理的分析"有了更深的了解,不妨来实际操作一番吧!这里是网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
线程
处理
分析
程序
显著
代码
任务
内容
图像
时间
学习
运行
实用
更深
不仅仅
事件
兴趣
单个
图像处理
在线下载
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
互联网科技公司排名
互联网科技的
麻工互联网科技
易互联网科技有限公司
平安科技 互联网
深圳互联网络科技有限公司
互联网科技排名
九亚互联网科技
华中科技大学互联网
深圳齐家互联网科技
须眉科技是互联网公司吗
团飞科技与伯堂互联网服务平台
翼展科技 首家互联网医院
互联网科技创新的路径
北京市互联网新科技
皮皮虾互联网科技有限公司
互联网安防科技感
互联网科技创新创业项目
互联网企业和网络科技有限公司
山东亿川互联网科技有限责任公司
互联网科技的英文
浩特互联网科技
融贸通互联网科技
恒生科技互联网指数分析
玖二柒互联网科技公司待遇
须眉科技是互联网公司吗
互联网5g黑科技有哪些
南博望互联网科技有限公司
徐州市互联网科技有限公司
风投北京互联网科技