Speed Up Slow requests.get() Calls in Python

Feb 3, 2024 ยท 2 min read

If you're using Python's requests library to make HTTP requests, you may occasionally run into performance issues where requests.get() seems to hang or take a very long time to complete. There are a few common reasons this can happen, along with some techniques you can use to speed things up.

Check for Network Issues

First, rule out any network or connectivity problems. Try making a request to a fast reliable website like google.com to see if other HTTP requests are slow or timing out. Issues like poor wifi connectivity, VPN routing, or firewall rules blocking requests could be the culprit.

Increase Timeout Value

By default, requests will timeout after a few seconds if a response isn't received. You can increase the timeout value to allow more time:

import requests

requests.get('http://slow-api.com', timeout=30) 

Just be careful not to set this too high, or your code may hang waiting for a server that will never respond.

Use Asynchronous Requests

If you need to make many requests in parallel, synchronous requests will bottleneck and queue up. The grequests library makes this easy to switch to asynchronous requests:

import grequests

urls = ['http://url1.com', 'http://url2.com']
reqs = (grequests.get(u) for u in urls)
grequests.map(reqs)

Now the requests can execute simultaneously and maximize network utilization.

Check for Slow APIs

Sometimes the problem is just a slow API you're calling out to. Profile the endpoint directly like with Postman to distinguish issues with your code vs slow external services. For public APIs, check status pages for degraded performance or rate limiting issues.

Optimizing slow requests takes some trial and error, but following network traces and profiling end-to-end usually reveals where the bottleneck lies. With some targeted tweaks you can often squeeze much better performance out of Python's versatile requests library.

Browse by tags:

Browse by language:

The easiest way to do Web Scraping

Get HTML from any page with a simple API call. We handle proxy rotation, browser identities, automatic retries, CAPTCHAs, JavaScript rendering, etc automatically for you


Try ProxiesAPI for free

curl "http://api.proxiesapi.com/?key=API_KEY&url=https://example.com"

<!doctype html>
<html>
<head>
    <title>Example Domain</title>
    <meta charset="utf-8" />
    <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
...

X

Don't leave just yet!

Enter your email below to claim your free API key: