METADATA 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. Metadata-Version: 2.0
  2. Name: retry
  3. Version: 0.9.2
  4. Summary: Easy to use retry decorator.
  5. Home-page: https://github.com/invl/retry
  6. Author: invl
  7. Author-email: invlpg@gmail.com
  8. License: Apache License 2.0
  9. Platform: UNKNOWN
  10. Classifier: Development Status :: 4 - Beta
  11. Classifier: Intended Audience :: Developers
  12. Classifier: License :: OSI Approved :: Apache Software License
  13. Classifier: Natural Language :: English
  14. Classifier: Operating System :: OS Independent
  15. Classifier: Programming Language :: Python
  16. Classifier: Programming Language :: Python :: 2.6
  17. Classifier: Programming Language :: Python :: 2.7
  18. Classifier: Programming Language :: Python :: 3
  19. Classifier: Programming Language :: Python :: 3.4
  20. Classifier: Programming Language :: Python :: Implementation :: PyPy
  21. Classifier: Topic :: Software Development
  22. Requires-Dist: decorator (>=3.4.2)
  23. Requires-Dist: py (<2.0.0,>=1.4.26)
  24. retry
  25. =====
  26. .. image:: https://pypip.in/d/retry/badge.png
  27. :target: https://pypi.python.org/pypi/retry/
  28. .. image:: https://pypip.in/v/retry/badge.png
  29. :target: https://pypi.python.org/pypi/retry/
  30. .. image:: https://pypip.in/license/retry/badge.png
  31. :target: https://pypi.python.org/pypi/retry/
  32. Easy to use retry decorator.
  33. Features
  34. --------
  35. - No external dependency (stdlib only).
  36. - (Optionally) Preserve function signatures (`pip install decorator`).
  37. - Original traceback, easy to debug.
  38. Installation
  39. ------------
  40. .. code-block:: bash
  41. $ pip install retry
  42. API
  43. ---
  44. retry decorator
  45. ^^^^^^^^^^^^^^^
  46. .. code:: python
  47. def retry(exceptions=Exception, tries=-1, delay=0, max_delay=None, backoff=1, jitter=0, logger=logging_logger):
  48. """Return a retry decorator.
  49. :param exceptions: an exception or a tuple of exceptions to catch. default: Exception.
  50. :param tries: the maximum number of attempts. default: -1 (infinite).
  51. :param delay: initial delay between attempts. default: 0.
  52. :param max_delay: the maximum value of delay. default: None (no limit).
  53. :param backoff: multiplier applied to delay between attempts. default: 1 (no backoff).
  54. :param jitter: extra seconds added to delay between attempts. default: 0.
  55. fixed if a number, random if a range tuple (min, max)
  56. :param logger: logger.warning(fmt, error, delay) will be called on failed attempts.
  57. default: retry.logging_logger. if None, logging is disabled.
  58. """
  59. Various retrying logic can be achieved by combination of arguments.
  60. Examples
  61. """"""""
  62. .. code:: python
  63. from retry import retry
  64. .. code:: python
  65. @retry()
  66. def make_trouble():
  67. '''Retry until succeed'''
  68. .. code:: python
  69. @retry(ZeroDivisionError, tries=3, delay=2)
  70. def make_trouble():
  71. '''Retry on ZeroDivisionError, raise error after 3 attempts, sleep 2 seconds between attempts.'''
  72. .. code:: python
  73. @retry((ValueError, TypeError), delay=1, backoff=2)
  74. def make_trouble():
  75. '''Retry on ValueError or TypeError, sleep 1, 2, 4, 8, ... seconds between attempts.'''
  76. .. code:: python
  77. @retry((ValueError, TypeError), delay=1, backoff=2, max_delay=4)
  78. def make_trouble():
  79. '''Retry on ValueError or TypeError, sleep 1, 2, 4, 4, ... seconds between attempts.'''
  80. .. code:: python
  81. @retry(ValueError, delay=1, jitter=1)
  82. def make_trouble():
  83. '''Retry on ValueError, sleep 1, 2, 3, 4, ... seconds between attempts.'''
  84. .. code:: python
  85. # If you enable logging, you can get warnings like 'ValueError, retrying in
  86. # 1 seconds'
  87. if __name__ == '__main__':
  88. import logging
  89. logging.basicConfig()
  90. make_trouble()
  91. retry_call
  92. ^^^^^^^^^^
  93. .. code:: python
  94. def retry_call(f, fargs=None, fkwargs=None, exceptions=Exception, tries=-1, delay=0, max_delay=None, backoff=1,
  95. jitter=0,
  96. logger=logging_logger):
  97. """
  98. Calls a function and re-executes it if it failed.
  99. :param f: the function to execute.
  100. :param fargs: the positional arguments of the function to execute.
  101. :param fkwargs: the named arguments of the function to execute.
  102. :param exceptions: an exception or a tuple of exceptions to catch. default: Exception.
  103. :param tries: the maximum number of attempts. default: -1 (infinite).
  104. :param delay: initial delay between attempts. default: 0.
  105. :param max_delay: the maximum value of delay. default: None (no limit).
  106. :param backoff: multiplier applied to delay between attempts. default: 1 (no backoff).
  107. :param jitter: extra seconds added to delay between attempts. default: 0.
  108. fixed if a number, random if a range tuple (min, max)
  109. :param logger: logger.warning(fmt, error, delay) will be called on failed attempts.
  110. default: retry.logging_logger. if None, logging is disabled.
  111. :returns: the result of the f function.
  112. """
  113. 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.
  114. .. code:: python
  115. import requests
  116. from retry.api import retry_call
  117. def make_trouble(service, info=None):
  118. if not info:
  119. info = ''
  120. r = requests.get(service + info)
  121. return r.text
  122. def what_is_my_ip(approach=None):
  123. if approach == "optimistic":
  124. tries = 1
  125. elif approach == "conservative":
  126. tries = 3
  127. else:
  128. # skeptical
  129. tries = -1
  130. result = retry_call(make_trouble, fargs=["http://ipinfo.io/"], fkwargs={"info": "ip"}, tries=tries)
  131. print(result)
  132. what_is_my_ip("conservative")