Flask or Requests is Hanging unless timeout is specified, and hangs until the specified timeout

I'm on a new computer and running into a strange issue:

Using requests without the timeout parameter simply hangs forever.

Using timeout parameter results in the request hanging until the value in timeout has passed; for example, if timeout is set to 2, the request hangs for two seconds; if timeout is set to 10; the request hangs for 10 seconds.

Using Firefox or Postman or etc. doesn't result in this behavior.

I'm on Python 3.6.5 using requests 2.19.1.

Flask code:

import flask

app = flask.Flask(__name__)

@app.route('/')
def home():
     return 'hello'

app.run(host='localhost', port=8080, debug=True)

Calling code:

import requests

# This is instant
requests.get('http://localhost:8080/', timeout=0.1)

# This hangs for 2 seconds
requests.get('http://localhost:8080/', timeout=2)

# This hangs for 10 seconds
requests.get('http://localhost:8080/', timeout=10)

# This hangs forever without a `timeout` param
requests.get('http://localhost:8080')

Using Firefox or Postman results in an instant response, so I figure it has something to do with requests.

On my old computer, I cannot replicate this behavior with the same versions of Python and requests.

Incidentally, when I first loaded this new computer about a month ago, I did not have a problem.

1 answer

  • answered 2019-07-11 18:06 Matthew Moisen

    After about three hours of debugging, I restarted my computer and the problem went away. I do not know the root cause.