Domanda

How does setting a timeout using Python's requests library work internally?

Is setting a timeout for a request actually setting a global timeout on the socket used by all other requests in the process?

In other words by using the timeout kwarg for a given request, am I affecting the timeouts for subsequent requests in the process?

È stato utile?

Soluzione

No, a timeout is set per created socket:

sock = socket.create_connection(
    address=(self.host, self.port),
    timeout=self.timeout,
)

This is all handled by urllib3, the lower-level library used by requests; see their API documentation:

timeout – Socket timeout in seconds for each individual connection. This can be a float or integer, which sets the timeout for the HTTP request, or an instance of urllib3.util.Timeout which gives you more fine-grained control over request timeouts. After the constructor has been parsed, this is always a urllib3.util.Timeout object.

requests makes use of the util.Timeout class for this to ensure both connecting and reading are subject to the timeout.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top