简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM