Skip to content

Commit fe5a6d9

Browse files
committed
Add an Example for automatic retries to the Advanced Usage docs
- While Requests doesn't automatically retry failures, this ability is a very common advanced use case in real world applications. - Although there's a mention of this ability on the HTTPAdapter class docs, it's a bit buried and not very specific. - It makes sense then to have an Example in the HTTPAdapter section of the Advanced Usage docs with a basic template for how this can be accomplished with Requests. - Open questions: -- is the urllib3.util.Retry import appropriate? I've seen some SO posts which import it from requests.adapters (even though that's just referring to urllib3)
1 parent d3d5044 commit fe5a6d9

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

docs/user/advanced.rst

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1026,8 +1026,30 @@ library to use SSLv3::
10261026
num_pools=connections, maxsize=maxsize,
10271027
block=block, ssl_version=ssl.PROTOCOL_SSLv3)
10281028

1029+
Example: Automatic Retries
1030+
^^^^^^^^^^^^^^^^^^^^^^^^^^
1031+
1032+
By default, Requests does not retry failed connections. However, it is possible
1033+
to implement automatic retries with a powerful array of features, including
1034+
backoff, within a Requests :class:`Session <requests.Session>` using the
1035+
`urllib3.util.Retry`_ class::
1036+
1037+
from urllib3.util import Retry
1038+
from requests import Session
1039+
from requests.adapters import HTTPAdapter
1040+
1041+
s = Session()
1042+
retries = Retry(
1043+
total=3,
1044+
backoff_factor=0.1,
1045+
status_forcelist=[502, 503, 504],
1046+
allowed_methods={'POST'},
1047+
)
1048+
s.mount('https://', HTTPAdapter(max_retries=retries))
1049+
10291050
.. _`described here`: https://kenreitz.org/essays/2012/06/14/the-future-of-python-http
10301051
.. _`urllib3`: https://github.com/urllib3/urllib3
1052+
.. _`urllib3.util.Retry`: https://urllib3.readthedocs.io/en/stable/reference/urllib3.util.html#Retry
10311053

10321054
.. _blocking-or-nonblocking:
10331055

0 commit comments

Comments
 (0)