通过用爬虫示例来说明并发相关的多线程、多进程、协程之间的执行效率对比。

假设我们现在要在网上下载图片,一个简单的方法是用 requests+BeautifulSoup。注:本文所有例子都使用python3.5)
单线程
示例 1:get_photos.py
import os
import time
import uuid
import requests
from bs4 import BeautifulSoup
def out_wrapper(func): # 记录程序执行时间的简单装饰器
def inner_wrapper():
start_time = time.time()
func()
stop_time = time.time()
print('Used time {}'.format(stop_time-start_time))
return inner_wrapper
def save_flag(img, filename): # 保存图片
path = os.path.join('down_photos', filename)
with open(path, 'wb') as fp:
fp.write(img)
def download_one(url): # 下载一个图片
image = requests.get(url)
save_flag(image.content, str(uuid.uuid4()))
def user_conf(): # 返回30个图片的url
url = 'https://unsplash.com/'
ret = requests.get(url)
soup = BeautifulSoup(ret.text, "lxml")
zzr = soup.find_all('img')
ret = []
num = 0
for item in zzr:
if item.get("src").endswith('80') and num < 30:
&nbs
Python并发编程:多线程、多进程与协程实践

本文介绍了Python中用于提高图片抓取效率的并发技术,包括单线程、多进程、多线程和协程(gevent与asyncio)。通过示例代码展示了它们在IO密集型任务中的性能对比,强调了多线程在IO密集型任务中的优势以及协程的单线程同步特性。最后,提供了Python学习交流群的信息。
最低0.47元/天 解锁文章
4973

被折叠的 条评论
为什么被折叠?



