README.rst 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. retry
  2. =====
  3. .. image:: https://pypip.in/d/retry/badge.png
  4. :target: https://pypi.python.org/pypi/retry/
  5. .. image:: https://pypip.in/v/retry/badge.png
  6. :target: https://pypi.python.org/pypi/retry/
  7. .. image:: https://pypip.in/license/retry/badge.png
  8. :target: https://pypi.python.org/pypi/retry/
  9. Easy to use retry decorator.
  10. Features
  11. --------
  12. - No external dependency (stdlib only).
  13. - (Optionally) Preserve function signatures (`pip install decorator`).
  14. - Original traceback, easy to debug.
  15. Installation
  16. ------------
  17. .. code-block:: bash
  18. $ pip install retry
  19. API
  20. ---
  21. retry decorator
  22. ^^^^^^^^^^^^^^^
  23. .. code:: python
  24. def retry(exceptions=Exception, tries=-1, delay=0, max_delay=None, backoff=1, jitter=0, logger=logging_logger):
  25. """Return a retry decorator.
  26. :param exceptions: an exception or a tuple of exceptions to catch. default: Exception.
  27. :param tries: the maximum number of attempts. default: -1 (infinite).
  28. :param delay: initial delay between attempts. default: 0.
  29. :param max_delay: the maximum value of delay. default: None (no limit).
  30. :param backoff: multiplier applied to delay between attempts. default: 1 (no backoff).
  31. :param jitter: extra seconds added to delay between attempts. default: 0.
  32. fixed if a number, random if a range tuple (min, max)
  33. :param logger: logger.warning(fmt, error, delay) will be called on failed attempts.
  34. default: retry.logging_logger. if None, logging is disabled.
  35. """
  36. Various retrying logic can be achieved by combination of arguments.
  37. Examples
  38. """"""""
  39. .. code:: python
  40. from retry import retry
  41. .. code:: python
  42. @retry()
  43. def make_trouble():
  44. '''Retry until succeed'''
  45. .. code:: python
  46. @retry(ZeroDivisionError, tries=3, delay=2)
  47. def make_trouble():
  48. '''Retry on ZeroDivisionError, raise error after 3 attempts, sleep 2 seconds between attempts.'''
  49. .. code:: python
  50. @retry((ValueError, TypeError), delay=1, backoff=2)
  51. def make_trouble():
  52. '''Retry on ValueError or TypeError, sleep 1, 2, 4, 8, ... seconds between attempts.'''
  53. .. code:: python
  54. @retry((ValueError, TypeError), delay=1, backoff=2, max_delay=4)
  55. def make_trouble():
  56. '''Retry on ValueError or TypeError, sleep 1, 2, 4, 4, ... seconds between attempts.'''
  57. .. code:: python
  58. @retry(ValueError, delay=1, jitter=1)
  59. def make_trouble():
  60. '''Retry on ValueError, sleep 1, 2, 3, 4, ... seconds between attempts.'''
  61. .. code:: python
  62. # If you enable logging, you can get warnings like 'ValueError, retrying in
  63. # 1 seconds'
  64. if __name__ == '__main__':
  65. import logging
  66. logging.basicConfig()
  67. make_trouble()
  68. retry_call
  69. ^^^^^^^^^^
  70. .. code:: python
  71. def retry_call(f, fargs=None, fkwargs=None, exceptions=Exception, tries=-1, delay=0, max_delay=None, backoff=1,
  72. jitter=0,
  73. logger=logging_logger):
  74. """
  75. Calls a function and re-executes it if it failed.
  76. :param f: the function to execute.
  77. :param fargs: the positional arguments of the function to execute.
  78. :param fkwargs: the named arguments of the function to execute.
  79. :param exceptions: an exception or a tuple of exceptions to catch. default: Exception.
  80. :param tries: the maximum number of attempts. default: -1 (infinite).
  81. :param delay: initial delay between attempts. default: 0.
  82. :param max_delay: the maximum value of delay. default: None (no limit).
  83. :param backoff: multiplier applied to delay between attempts. default: 1 (no backoff).
  84. :param jitter: extra seconds added to delay between attempts. default: 0.
  85. fixed if a number, random if a range tuple (min, max)
  86. :param logger: logger.warning(fmt, error, delay) will be called on failed attempts.
  87. default: retry.logging_logger. if None, logging is disabled.
  88. :returns: the result of the f function.
  89. """
  90. This is very similar to the decorator, except that it takes a function and its arguments as parameters. The use case behind it is to be able to dynamically adjust the retry arguments.
  91. .. code:: python
  92. import requests
  93. from retry.api import retry_call
  94. def make_trouble(service, info=None):
  95. if not info:
  96. info = ''
  97. r = requests.get(service + info)
  98. return r.text
  99. def what_is_my_ip(approach=None):
  100. if approach == "optimistic":
  101. tries = 1
  102. elif approach == "conservative":
  103. tries = 3
  104. else:
  105. # skeptical
  106. tries = -1
  107. result = retry_call(make_trouble, fargs=["http://ipinfo.io/"], fkwargs={"info": "ip"}, tries=tries)
  108. print(result)
  109. what_is_my_ip("conservative")