Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python requests library is very slow on Windows

This problem occurs on Windows 10 (I know this, because it's not present on Mac OS).

Whenever you do a simple request to one of your endpoints like the following, it's extremely slow on Windows, whereas it's nearly instant on Mac OS.

import requests
requests.get('/user/get/' + str(current_user.id))

It takes about 3 seconds on Windows, while on Mac OS, it's nearly instantaneous.

By using some simple logging, I found that urllib3 (which is the underlying library for requests) takes a long time when making a new http connection. This is currently the pain point, and I don't have the knowledge to understand how to fix this.

DEBUG:urllib3.connectionpool:Starting new HTTP connection (1): localhost:5004

Failed fix 1:

I tried the following from this answer, but it does not work.

import requests

session = requests.Session()
session.trust_env = False

r = session.get('/user/get/' + str(current_user.id))

I also tried the following from the same answer, and it does not work.

os.environ['NO_PROXY'] = 'localhost'

Failed fix 2:

I tried a second supposed fix, and it did not work either.

import requests

proxies = {
  "http": None,
  "https": None,
}

r = requests.get('/user/get/' + str(current_user.id), proxies=proxies)

Now..

This leaves me with no answer and no available fix. Does someone know why this is an issue?

like image 327
Casper Hansen Avatar asked Mar 19 '26 06:03

Casper Hansen


1 Answers

I found out that this is a DNS issue from this github issue. As specified in the answer, you must change localhost to 127.0.0.1, and it is NOT a requests issue.

like image 110
Casper Hansen Avatar answered Mar 24 '26 11:03

Casper Hansen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!