{ "info": { "author": "Bob Green, Alexandr Skurikhin", "author_email": "bgreen@litl.com, a.skurihin@gmail.com", "bugtrack_url": null, "classifiers": [ "Development Status :: 5 - Production/Stable", "Intended Audience :: Developers", "License :: OSI Approved :: MIT License", "Natural Language :: English", "Operating System :: OS Independent", "Programming Language :: Python", "Programming Language :: Python :: 3.5", "Programming Language :: Python :: Implementation", "Programming Language :: Python :: Implementation :: CPython", "Topic :: Internet :: WWW/HTTP", "Topic :: Software Development :: Libraries :: Python Modules", "Topic :: Utilities" ], "description": "backoff\n=======\n\n.. image:: https://travis-ci.org/litl/backoff.svg?branch=master\n :target: https://travis-ci.org/litl/backoff?branch=master\n.. image:: https://coveralls.io/repos/litl/backoff/badge.svg?branch=master\n :target: https://coveralls.io/r/litl/backoff?branch=master\n\n**Function decoration for backoff and retry**\n\nThis module provides function decorators which can be used to wrap a\nfunction such that it will be retried until some condition is met. It\nis meant to be of use when accessing unreliable resources with the\npotential for intermittent failures i.e. network resources and external\nAPIs. Somewhat more generally, it may also be of use for dynamically\npolling resources for externally generated content.\n\nExamples\n========\n\nSince Kenneth Reitz's `requests `_ module\nhas become a defacto standard for HTTP clients in python, networking\nexamples below are written using it, but it is in no way required by\nthe backoff module.\n\n@backoff.on_exception\n---------------------\n\nThe ``on_exception`` decorator is used to retry when a specified exception\nis raised. Here's an example using exponential backoff when any\n``requests`` exception is raised::\n\n @backoff.on_exception(backoff.expo,\n requests.exceptions.RequestException,\n max_tries=8)\n def get_url(url):\n return requests.get(url)\n\nThe decorator will also accept a tuple of exceptions for cases where\nyou want the same backoff behavior for more than one exception type::\n\n @backoff.on_exception(backoff.expo,\n (requests.exceptions.Timeout,\n requests.exceptions.ConnectionError),\n max_tries=8)\n def get_url(url):\n return requests.get(url)\n\nIn some cases the raised exception instance itself may need to be\ninspected in order to determine if it is a retryable condition. The\n``giveup`` keyword arg can be used to specify a function which accepts\nthe exception and returns a truthy value if the exception should not\nbe retried::\n\n def fatal_code(e):\n return 400 <= e.response.status_code < 500\n\n @backoff.on_exception(backoff.expo,\n requests.exceptions.RequestException,\n max_tries=8,\n giveup=fatal_code)\n def get_url(url):\n return requests.get(url)\n\n\n@backoff.on_predicate\n---------------------\n\nThe ``on_predicate`` decorator is used to retry when a particular\ncondition is true of the return value of the target function. This may\nbe useful when polling a resource for externally generated content.\n\nHere's an example which uses a fibonacci sequence backoff when the\nreturn value of the target function is the empty list::\n\n @backoff.on_predicate(backoff.fibo, lambda x: x == [], max_value=13)\n def poll_for_messages(queue):\n return queue.get()\n\nExtra keyword arguments are passed when initializing the\nwait generator, so the ``max_value`` param above is passed as a keyword\narg when initializing the fibo generator.\n\nWhen not specified, the predicate param defaults to the falsey test,\nso the above can more concisely be written::\n\n @backoff.on_predicate(backoff.fibo, max_value=13)\n def poll_for_message(queue)\n return queue.get()\n\nMore simply, a function which continues polling every second until it\ngets a non-falsey result could be defined like like this::\n\n @backoff.on_predicate(backoff.constant, interval=1)\n def poll_for_message(queue)\n return queue.get()\n\nJitter\n------\n\nA jitter algorithm can be supplied with the ``jitter`` keyword arg to\neither of the backoff decorators. This argument should be a function\naccepting the original unadulterated backoff value and returning it's\njittered counterpart.\n\nAs of version 1.2, the default jitter function ``backoff.full_jitter``\nimplements the 'Full Jitter' algorithm as defined in the AWS\nArchitecture Blog's `Exponential Backoff And Jitter\n`_ post.\n\nPrevious versions of backoff defaulted to adding some random number of\nmilliseconds (up to 1s) to the raw sleep value. If desired, this\nbehavior is now available as ``backoff.random_jitter``.\n\nUsing multiple decorators\n-------------------------\n\nThe backoff decorators may also be combined to specify different\nbackoff behavior for different cases::\n\n @backoff.on_predicate(backoff.fibo, max_value=13)\n @backoff.on_exception(backoff.expo,\n requests.exceptions.HTTPError,\n max_tries=4)\n @backoff.on_exception(backoff.expo,\n requests.exceptions.TimeoutError,\n max_tries=8)\n def poll_for_message(queue):\n return queue.get()\n\nRuntime Configuration\n---------------------\n\nThe decorator functions ``on_exception`` and ``on_predicate`` are\ngenerally evaluated at import time. This is fine when the keyword args\nare passed as constant values, but suppose we want to consult a\ndictionary with configuration options that only become available at\nruntime. The relevant values are not available at import time. Instead,\ndecorator functions can be passed callables which are evaluated at\nruntime to obtain the value::\n\n def lookup_max_tries():\n # pretend we have a global reference to 'app' here\n # and that it has a dictionary-like 'config' property\n return app.config[\"BACKOFF_MAX_TRIES\"]\n\n @backoff.on_exception(backoff.expo,\n ValueError,\n max_tries=lookup_max_tries)\n\nMore cleverly, you might define a function which returns a lookup\nfunction for a specified variable::\n\n def config(app, name):\n return functools.partial(app.config.get, name)\n\n @backoff.on_exception(backoff.expo,\n ValueError,\n max_value=config(app, \"BACKOFF_MAX_VALUE\")\n max_tries=config(app, \"BACKOFF_MAX_TRIES\"))\n\nEvent handlers\n--------------\n\nBoth backoff decorators optionally accept event handler functions\nusing the keyword arguments ``on_success``, ``on_backoff``, and ``on_giveup``.\nThis may be useful in reporting statistics or performing other custom\nlogging.\n\nHandlers must be callables with a unary signature accepting a dict\nargument. This dict contains the details of the invocation. Valid keys\ninclude:\n\n* *target*: reference to the function or method being invoked\n* *args*: positional arguments to func\n* *kwargs*: keyword arguments to func\n* *tries*: number of invocation tries so far\n* *wait*: seconds to wait (``on_backoff`` handler only)\n* *value*: value triggering backoff (``on_predicate`` decorator only)\n\nA handler which prints the details of the backoff event could be\nimplemented like so::\n\n def backoff_hdlr(details):\n print (\"Backing off {wait:0.1f} seconds afters {tries} tries \"\n \"calling function {func} with args {args} and kwargs \"\n \"{kwargs}\".format(**details))\n\n @backoff.on_exception(backoff.expo,\n requests.exceptions.RequestException,\n on_backoff=backoff_hdlr)\n def get_url(url):\n return requests.get(url)\n\n**Multiple handlers per event type**\n\nIn all cases, iterables of handler functions are also accepted, which\nare called in turn. For example, you might provide a simple list of\nhandler functions as the value of the ``on_backoff`` keyword arg::\n\n @backoff.on_exception(backoff.expo,\n requests.exceptions.RequestException,\n on_backoff=[backoff_hdlr1, backoff_hdlr2])\n def get_url(url):\n return requests.get(url)\n\n**Getting exception info**\n\nIn the case of the ``on_exception`` decorator, all ``on_backoff`` and\n``on_giveup`` handlers are called from within the except block for the\nexception being handled. Therefore exception info is available to the\nhandler functions via the python standard library, specifically\n``sys.exc_info()`` or the ``traceback`` module.\n\nLogging configuration\n---------------------\n\nErrors and backoff and retry attempts are logged to the 'backoff'\nlogger. By default, this logger is configured with a NullHandler, so\nthere will be nothing output unless you configure a handler.\nProgrammatically, this might be accomplished with something as simple\nas::\n\n logging.getLogger('backoff').addHandler(logging.StreamHandler())\n\nThe default logging level is ERROR, which corresponds to logging anytime\n``max_tries`` is exceeded as well as any time a retryable exception is\nraised. If you would instead like to log any type of retry, you can\nset the logger level to INFO::\n\n logging.getLogger('backoff').setLevel(logging.INFO)", "description_content_type": null, "docs_url": null, "download_url": "", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/a.sk/backoff-async", "keywords": "backoff function decorator async", "license": "MIT", "maintainer": "Alexandr Skurikhin", "maintainer_email": "a.skurihin@gmail.com", "name": "backoff-async", "package_url": "https://pypi.org/project/backoff-async/", "platform": "UNKNOWN", "project_url": "https://pypi.org/project/backoff-async/", "project_urls": { "Homepage": "https://github.com/a.sk/backoff-async" }, "release_url": "https://pypi.org/project/backoff-async/2.0.0/", "requires_dist": null, "requires_python": null, "summary": "Function decoration for backoff and retry async functions", "version": "2.0.0" }, "last_serial": 2363731, "releases": { "2.0.0": [ { "comment_text": "", "digests": { "md5": "b185637881ab05e479b48c3b7c10d3c8", "sha256": "953254ed20c8743b4358df9c0b4fd09d59ff1882bb4725848b9e80ff5820c4fe" }, "downloads": -1, "filename": "backoff-async-2.0.0.tar.gz", "has_sig": false, "md5_digest": "b185637881ab05e479b48c3b7c10d3c8", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 7255, "upload_time": "2016-09-26T10:04:01", "url": "https://files.pythonhosted.org/packages/e4/36/a3c9dc5a3ba3c6c40c6b199957a60933fa2b82aebbaed49443ca23657a50/backoff-async-2.0.0.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "b185637881ab05e479b48c3b7c10d3c8", "sha256": "953254ed20c8743b4358df9c0b4fd09d59ff1882bb4725848b9e80ff5820c4fe" }, "downloads": -1, "filename": "backoff-async-2.0.0.tar.gz", "has_sig": false, "md5_digest": "b185637881ab05e479b48c3b7c10d3c8", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 7255, "upload_time": "2016-09-26T10:04:01", "url": "https://files.pythonhosted.org/packages/e4/36/a3c9dc5a3ba3c6c40c6b199957a60933fa2b82aebbaed49443ca23657a50/backoff-async-2.0.0.tar.gz" } ] }