下载安卓APP箭头
箭头给我发消息

客服QQ:3315713922

编程语言中多线程和多处理的初学者指南

作者:机器学习与数据分析     来源: 今日头条点击数:1003发布时间: 2020-05-03 16:24:34

标签: 编程语言Pythonjava

大神带你学编程,欢迎选课

Python中多线程和多处理的初学者指南。编程语言(programming language)是一种被标准化的交流技巧,用来向计算机发出指令,定义计算机程序,让程序员能够准确地定义计算机所需要使用的数据,并精确地定义在不同情况下所应当采取的行动的一种计算机语言。 编程语言可以分成机器语言、汇编语言、高级语言三大类。计算机领域已发明了上千不同的编程语言,而且每年仍有新的编程语言诞生。

即将开播:4月29日,民生银行郭庆谈商业银行金融科技赋能的探索与实践

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

在这篇文章中,我们不会详细讨论多线程或多处理的内部原理。相反,我们举一个例子,编写一个小的Python脚本从Unsplash下载图像。我们将从一次下载一个图像的版本开始。接下来,我们使用线程来提高执行速度。

编程语言中多线程和多处理的初学者指南_编程语言_Python_java_课课家

多线程

简单地说,线程允许您并行地运行程序。花费大量时间等待外部事件的任务通常适合线程化。它们也称为I/O Bound任务例如从文件中读写,网络操作或使用API在线下载。让我们来看一个示例,它展示了使用线程的好处。

1. 没有线程

在本例中,我们希望通过顺序运行程序来查看从Unsplash API下载15张图像需要多长时间:

  1. import requests 
  2. import time 
  3. img_urls = [ 
  4.     'https://images.unsplash.com/photo-1516117172878-fd2c41f4a759', 
  5.     'https://images.unsplash.com/photo-1532009324734-20a7a5813719', 
  6.     'https://images.unsplash.com/photo-1524429656589-6633a470097c', 
  7.     'https://images.unsplash.com/photo-1530224264768-7ff8c1789d79', 
  8.     'https://images.unsplash.com/photo-1564135624576-c5c88640f235', 
  9.     'https://images.unsplash.com/photo-1541698444083-023c97d3f4b6', 
  10.     'https://images.unsplash.com/photo-1522364723953-452d3431c267', 
  11.     'https://images.unsplash.com/photo-1513938709626-033611b8cc03', 
  12.     'https://images.unsplash.com/photo-1507143550189-fed454f93097', 
  13.     'https://images.unsplash.com/photo-1493976040374-85c8e12f0c0e', 
  14.     'https://images.unsplash.com/photo-1504198453319-5ce911bafcde', 
  15.     'https://images.unsplash.com/photo-1530122037265-a5f1f91d3b99', 
  16.     'https://images.unsplash.com/photo-1516972810927-80185027ca84', 
  17.     'https://images.unsplash.com/photo-1550439062-609e1531270e', 
  18.     'https://images.unsplash.com/photo-1549692520-acc6669e2f0c' 
  19.  
  20. start = time.perf_counter() #start timer 
  21. for img_url in img_urls: 
  22.     img_name = img_url.split('/')[3] #get image name from url 
  23.     img_bytes = requests.get(img_url).content 
  24. with open(img_name, 'wb') as img_file: 
  25.      img_file.write(img_bytes) #save image to disk  
  26.  
  27. finish = time.perf_counter() #end timer 
  28. print(f"Finished in {round(finish-start,2)} seconds")  
  29.  
  30. #results 
  31. Finished in 23.101926751 seconds 

一共用时​23秒。

2. 多线程

让我们看看Pyhton中的线程模块如何显著地改进我们的程序执行:

  1. import time 
  2. from concurrent.futures import ThreadPoolExecutor 
  3.  
  4. def download_images(url): 
  5.     img_name = img_url.split('/')[3] 
  6.     img_bytes = requests.get(img_url).content 
  7.     with open(img_name, 'wb') as img_file: 
  8.          img_file.write(img_bytes) 
  9.          print(f"{img_name} was downloaded") 
  10.  
  11. start = time.perf_counter() #start timer 
  12. with ThreadPoolExecutor() as executor: 
  13.     results = executor.map(download_images,img_urls) #this is Similar to map(func, *iterables) 
  14. finish = time.perf_counter() #end timer 
  15. print(f"Finished in {round(finish-start,2)} seconds") 
  16.  
  17. #results  
  18. Finished in 5.544147536 seconds 

我们可以看到,与不使用线程代码相比,使用线程代码可以显著提高速度。从23秒到5秒。

对于本例,请注意在创建线程时存在开销,因此将线程用于多个API调用是有意义的,而不仅仅是单个调用。

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

编程语言原本是被设计成专门使用在计算机上的,但它们也可以用来定义算法或者数据结构。正是因为如此,程序员才会试图使程序代码更容易阅读。

赞(14)
踩(0)
分享到:
华为认证网络工程师 HCIE直播课视频教程