1
votes

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?

1
Is there a DNS problem? Sometimes code wants to resolve localhost or something and it takes a long time to fail if it's badly configured. (Thankfully no Windows10 experience though.) - tripleee
@tripleee yes i just found the problem! See my answer. Changing localhost to 127.0.0.1 resolves the issue. Also, the request does not fail, it goes through, it is just very slow (for some reason). - Casper Hansen
having this same issue, and I'm using an actual server on the net, not localhost... - tremor

1 Answers

2
votes

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.