1.软文推荐

2.软文推荐

3.软文推荐

使用Python分析数据,如果使用了正确的数据结构和算法,有时可以大量提高程序的速度。实现此目的的一种方法是使用Muiltithreading(多线程)或Multiprocessing(多重处理)。

多线程

简单地说,线程允许您并行地运行程序。花费大量时间等待外部事件的任务通常适合线程化。它们也称为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调用是有意义的,而不仅仅是单个调用。

此外,对于密集的计算,如数据处理,图像处理多处理比线程执行得更好。

本文来源:www.lxlinux.net/8462.html,若引用不当,请联系修改。

相关文章 8

1

做58同城有效果吗?(58同城推广效果怎么样) 3分钟前

58.这个平台见仁见智,从理性的角度我们来分析一下。 1.这是一个大众的服务平台,说没有效果吧,还是太片面了。毕竟58还是每年有投入这...

2

CYUN:2021年盛夏来袭,香港/美国云服务器新购8.5折,低至24.65元/月;香港/美国独服3-5IP仅46 5分钟前

cyun怎么样? cyun主营香港、日本、韩国、美国服务器资源,实体公司,ISP/IDC资质齐全,客服配备齐全。现在CYUN推出2021年盛夏促销活动,全...

3

Linux系统创建软连接命令 8分钟前

Linux软链接,类似于windows系统的快捷键。譬如你将windows系统的D盘中某一个文件夹放在桌面上当做快捷键。当在不同目录使用相同文件时,可...

4

宝塔如何安装Redis?宝塔面板安装配置Redis教程(宝塔如何安装web服务器) 11分钟前

宝塔如何安装Redis? Redis是一个开源的使用ANSI C语言编写、支持网络、可基于内存亦可持久化的日志型、Key-Value数据库,并提供多种语言的...

5

Linux常用命令—groupadd命令 13分钟前

Linux常用命令 groupadd命令 用于创建一个新的工作组,新工作组的信息将被添加到系统文件中,下面良许教程网为大家分享一下Linux常用命令...

6

加固Redis服务安全具体方法 16分钟前

Redis是当前比较热门的NOSQL系统之一,它是一个开源的使用ANSI c语言编写的 key-value 存储系统(区别于MySQL的二维表格的形式存储。)。和Me...

7

通过命令行的方式查找公网动态 IP 地址 17分钟前

公网IP是运行商给你的IP,是互联网上的一个门牌号。内网IP一般是你的局域网IP,比如你家有路由器,就必须有一个内网IP,是内网的一个门...

8

简单讲解一下python模块之calendar 20分钟前

calendar,是与日历相关的模块。calendar模块文件里定义了很多类型,主要有Calendar,TextCalendar以及HTMLCalendar类型。其中,Calendar是TextCalendar与...