1from __future__
import absolute_import
6from socket
import _GLOBAL_DEFAULT_TIMEOUT, getdefaulttimeout
8from ..exceptions
import TimeoutStateError
20 """Timeout configuration.
22 Timeouts can be defined as a default for a pool:
24 .. code-block:: python
26 timeout = Timeout(connect=2.0, read=7.0)
27 http = PoolManager(timeout=timeout)
28 response = http.request('GET', 'http://example.com/')
30 Or per-request (which overrides the default for the pool):
32 .. code-block:: python
34 response = http.request('GET', 'http://example.com/', timeout=Timeout(10))
36 Timeouts can be disabled by setting all the parameters to ``None``:
38 .. code-block:: python
40 no_timeout = Timeout(connect=None, read=None)
41 response = http.request('GET', 'http://example.com/, timeout=no_timeout)
45 This combines the connect and read timeouts into one; the read timeout
46 will be set to the time leftover from the connect attempt. In the
47 event that both a connect timeout and a total are specified, or a read
48 timeout and a total are specified, the shorter timeout will be applied.
52 :type total: int, float, or None
55 The maximum amount of time (in seconds) to wait for a connection
56 attempt to a server to succeed. Omitting the parameter will default the
57 connect timeout to the system default, probably `the global default
59 <http://hg.python.org/cpython/file/603b4d593758/Lib/socket.py#l535>`_.
60 None will set an infinite timeout for connection attempts.
62 :type connect: int, float, or None
65 The maximum amount of time (in seconds) to wait between consecutive
66 read operations for a response from the server. Omitting the parameter
67 will default the read timeout to the system default, probably `the
68 global default timeout in socket.py
69 <http://hg.python.org/cpython/file/603b4d593758/Lib/socket.py#l535>`_.
70 None will set an infinite timeout.
72 :type read: int, float, or None
76 Many factors can affect the total amount of time for urllib3 to return
79 For example, Python's DNS resolver does not obey the timeout specified
80 on the socket. Other factors that can affect total request time include
81 high CPU load, high swap, the program running at a low priority level,
84 In addition, the read and total timeouts only measure the time between
85 read operations on the socket connecting the client and the server,
86 not the total amount of time for the request to return a complete
87 response. For most requests, the timeout is raised because the server
88 has not sent the first byte in the specified time. This is not always
89 the case; if a server streams one byte every fifteen seconds, a timeout
90 of 20 seconds will not trigger, even though the request will take
91 several minutes to complete.
93 If your goal is to cut off any request after a set amount of wall clock
94 time, consider having a second "watcher" thread to cut off a slow
99 DEFAULT_TIMEOUT = _GLOBAL_DEFAULT_TIMEOUT
101 def __init__(self, total=None, connect=_Default, read=_Default):
108 return "%s(connect=%r, read=%r, total=%r)" % (
120 return getdefaulttimeout()
if timeout
is cls.
DEFAULT_TIMEOUT else timeout
124 """Check that a timeout attribute is valid.
126 :param value: The timeout value to validate
127 :param name: The name of the timeout attribute to validate. This is
128 used to specify in error messages.
129 :return: The validated and casted version of the given value.
130 :raises ValueError: If it is a numeric value less than or equal to
131 zero, or the type is not an integer, float, or None.
133 if value
is _Default:
141 "Timeout cannot be a boolean value. It must "
142 "be an int, float or None."
146 except (TypeError, ValueError):
148 "Timeout value %s was %s, but it must be an "
149 "int, float or None." % (name, value)
155 "Attempted to set %s timeout to %s, but the "
156 "timeout cannot be set to a value less "
157 "than or equal to 0." % (name, value)
162 "Timeout value %s was %s, but it must be an "
163 "int, float or None." % (name, value)
170 """Create a new Timeout from a legacy timeout value.
172 The timeout value used by httplib.py sets the same timeout on the
173 connect(), and recv() socket requests. This creates a :class:`Timeout`
174 object that sets the individual timeouts to the ``timeout`` value
175 passed to this function.
177 :param timeout: The legacy timeout value.
178 :type timeout: integer, float, sentinel default object, or None
179 :return: Timeout object
180 :rtype: :class:`Timeout`
182 return Timeout(read=timeout, connect=timeout)
185 """Create a copy of the timeout object
187 Timeout properties are stored per-pool but each request needs a fresh
188 Timeout object to ensure each one has its own start/stop configured.
190 :return: a copy of the timeout object
191 :rtype: :class:`Timeout`
199 """Start the timeout clock, used during a connect() attempt
201 :raises urllib3.exceptions.TimeoutStateError: if you attempt
202 to start a timer that has been started already.
210 """Gets the time elapsed since the call to :meth:`start_connect`.
212 :return: Elapsed time in seconds.
214 :raises urllib3.exceptions.TimeoutStateError: if you attempt
215 to get duration for a timer that hasn't been started.
219 "Can't get connect duration for timer that has not started."
225 """Get the value to use when setting a connection timeout.
227 This will be a positive float or integer, the value None
228 (never timeout), or the default system timeout.
230 :return: Connect timeout.
231 :rtype: int, float, :attr:`Timeout.DEFAULT_TIMEOUT` or None
233 if self.
total is None:
243 """Get the value for the read timeout.
245 This assumes some time has elapsed in the connection timeout and
246 computes the read timeout appropriately.
248 If self.total is set, the read timeout is dependent on the amount of
249 time taken by the connect timeout. If the connection time has not been
250 established, a :exc:`~urllib3.exceptions.TimeoutStateError` will be
253 :return: Value to use for the read timeout.
254 :rtype: int, float, :attr:`Timeout.DEFAULT_TIMEOUT` or None
255 :raises urllib3.exceptions.TimeoutStateError: If :meth:`start_connect`
256 has not yet been called on this object.
259 self.
total is not None
261 and self.
_read is not None
get_connect_duration(self)
resolve_default_timeout(cls, timeout)
__init__(self, total=None, connect=_Default, read=_Default)
_validate_timeout(cls, value, name)