How to force timeout in python query library (including DNS lookup)

The Python query library only supports connect timeout and read http://docs.python-requests.org/en/master/user/advanced/#timeouts

It is not possible to force a timeout when a DNS lookup takes a very long time. I would like to start a timeout when it takes more than X seconds to complete the GET request (including DNS, connect and read).

Please note that I cannot use the signal approach , as it only works in the main thread.

I am looking for an elegant solution.

+6
source share
1 answer

, getaddrinfo C, .

, IMHO - , , timeout_decorator:

@timeout_decorator.timeout(5, use_signals=False)
def timed_get(url):
  return requests.get(url)

, .

+2

All Articles