惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

MyScale Blog
MyScale Blog
A
About on SuperTechFans
G
Google Developers Blog
B
Blog RSS Feed
F
Fortinet All Blogs
WordPress大学
WordPress大学
Recent Announcements
Recent Announcements
Hugging Face - Blog
Hugging Face - Blog
Y
Y Combinator Blog
MongoDB | Blog
MongoDB | Blog
小众软件
小众软件
人人都是产品经理
人人都是产品经理
博客园 - 叶小钗
T
The Blog of Author Tim Ferriss
Jina AI
Jina AI
IT之家
IT之家
P
Proofpoint News Feed
美团技术团队
量子位
Microsoft Azure Blog
Microsoft Azure Blog
Engineering at Meta
Engineering at Meta
B
Blog
有赞技术团队
有赞技术团队
U
Unit 42

jdhao's digital space

Conversion between base64 and OpenCV or PIL Image 腾讯云对象存储博客图床开启 CDN 加速(不需要购买额外域名) Search and Replace in Multiple Files in Vim/Neovim Change Table Column Width in LaTeX Image or Table Side by Side in LaTeX LaTeX 并排显示图像或表格 Firenvim: Neovim inside Your Browser Content inside HTML tags missing in Latest Hugo? Creating Markdown Front Matter with Ultisnips Labelme JSON 标注格式转 voc XML 格式 Nifty Nvim Techniques That Make My Life Easier -- Series 6 macOS 下如何为视频制作字幕 Running Command Asynchronously inside Neovim Resolving Merge Conflict after Git Stash Pop Pylint: command not found? A Hands-on Experience with Neovim's Built-in LSP Support How to Convert PDF to Images with Imagemagick 互联网上常用缩略语集锦 File Backup in Neovim Converting PDF Pages to Images with Poppler Nifty Nvim Techniques That Make My Life Easier -- Series 5 Neovim Configuration for System-wide Use How to sort a list of tuple or list in Python -- lambda or itemgetter? Building A Vim Statusline from Scratch 人类第一颗原子弹爆炸始末 Distributed Training in PyTorch with Horovod Learning Expect Programming Essential Knowledge about SSH Nifty LaTeX Techniques -- Series 1 更改 Adsense 邮寄地址,重新寄送 PIN
Downloading Images Faster with requests Sessions
2020-06-21 · via jdhao's digital space

In my previous post, I write about how to download an image from URL using requests. In this post, I want to share ways to make the download speed faster.

Various ways exist for downloading an image using the requests package.

Persistent session or not?#

There are two ways to make an HTTP request using requests:

The difference is that with requests.get(), a new session object is created each time when we make a request. It means that each time we make the request, we need to establish a new TCP connection to the remote host. The connection is closed when we finish the request. Since establishing a connection to the server takes some time, downloading images using requests.get() causes extra overhead.

With session.get(), if we are making several requests to the same host, the connection will be reused so that no time is wasted re-establishing new connections. This is also known as HTTP persistent connection. In summary, using session will reduce the image download time.

stream or not?#

As said in the previous post, for large files, we may want to use stream parameter when making the request, which will reduce the memory overhead. So we have two ways to get the binary image from the response:

  • using response.content
  • using response.raw.read()

Benchmark#

By combining session options and stream options, we have four different ways to download images using requests:

  • r.raw with session
  • r.content with session
  • r.raw without session
  • r.content without session

In order to find which is faster, I have run a small benchmark. I combine concurrent.futures and requests to download several images concurrently using the above four different settings of requests. The complete code can be found here.

According to benchmark result, using sessions is faster than requests without explicit sessions. For the stream option, using r.raw is generally faster than using r.content, but it is not always the case. If the image size not big enough, using either r.raw or r.content is fine.

The benefit of using explicit sessions is more apparent when we are downloading more images concurrently. On my Mac, when downing 20 images concurrently, I get the following result:

avg time (r.raw with session): 0.2751150131225586
avg time (r.content with session): 0.2750370740890503
avg time (r.raw no session): 1.5932393550872803
avg time (r.content no session): 0.8408806085586548

With 40 images, the output is:

avg time (r.raw with session): 0.3991949200630188
avg time (r.content with session): 0.42252095937728884
avg time (r.raw no session): 1.937515652179718
avg time (r.content no session): 1.912631618976593

Conclusion#

From the above results, we can conclude that using requests with sessions will reduce image download time immensely.

References#