ratelimiter


Nameratelimiter JSON
Version 1.2.0.post0 PyPI version JSON
download
home_pagehttps://github.com/RazerM/ratelimiter
SummarySimple python rate limiting object
upload_time2017-12-12 00:33:38
maintainer
docs_urlNone
authorFrazer McLean
requires_python
licenseApache
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI
coveralls test coverage No coveralls.
            RateLimiter
===========

|PyPI Version| |Build Status| |Python Version| |License|

Simple Python module providing rate limiting.

Overview
--------

This package provides the ``ratelimiter`` module, which ensures that an
operation will not be executed more than a given number of times on a
given period. This can prove useful when working with third parties APIs
which require for example a maximum of 10 requests per second.

Usage
-----

Decorator
~~~~~~~~~

.. code:: python

    from ratelimiter import RateLimiter

    @RateLimiter(max_calls=10, period=1)
    def do_something():
        pass

Context Manager
~~~~~~~~~~~~~~~

.. code:: python

    from ratelimiter import RateLimiter

    rate_limiter = RateLimiter(max_calls=10, period=1)

    for i in range(100):
        with rate_limiter:
            do_something()

Callback
~~~~~~~~

The callback is called in its own thread, so your callback may use
``sleep`` without delaying the rate limiter.

.. code:: python

    import time

    from ratelimiter import RateLimiter

    def limited(until):
        duration = int(round(until - time.time()))
        print('Rate limited, sleeping for {:d} seconds'.format(duration))

    rate_limiter = RateLimiter(max_calls=2, period=3, callback=limited)

    for i in range(3):
        with rate_limiter:
            print('Iteration', i)

Output:

::

    Iteration 0
    Iteration 1
    Rate limited, sleeping for 3 seconds
    Iteration 2

asyncio
~~~~~~~

The ``RateLimiter`` object can be used in an ``async with`` statement on
Python 3.5+. Note that the callback must be a coroutine in this context.
The coroutine callback is not called in a separate thread.

.. code:: python

    import asyncio
    import time

    from ratelimiter import RateLimiter

    async def limited(until):
        duration = int(round(until - time.time()))
        print('Rate limited, sleeping for {:d} seconds'.format(duration))

    async def coro():
        rate_limiter = RateLimiter(max_calls=2, period=3, callback=limited)
        for i in range(3):
            async with rate_limiter:
                print('Iteration', i)

    loop = asyncio.get_event_loop()
    loop.run_until_complete(coro())

License
-------

| Original work Copyright 2013 Arnaud Porterie
| Modified work Copyright 2016 Frazer McLean

Licensed under the Apache License, Version 2.0 (the “License”); you may
not use this file except in compliance with the License. You may obtain
a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an “AS IS” BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

.. |PyPI Version| image:: http://img.shields.io/pypi/v/ratelimiter.svg?style=flat-square
   :target: https://pypi.python.org/pypi/ratelimiter
.. |Build Status| image:: http://img.shields.io/travis/RazerM/ratelimiter/master.svg?style=flat-square
   :target: https://travis-ci.org/RazerM/ratelimiter
.. |Python Version| image:: https://img.shields.io/badge/python-2.7%2C%203-brightgreen.svg?style=flat-square
   :target: https://www.python.org/downloads/
.. |License| image:: http://img.shields.io/badge/license-Apache-blue.svg?style=flat-square
   :target: https://github.com/RazerM/ratelimiter/blob/master/LICENSE



            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/RazerM/ratelimiter",
    "name": "ratelimiter",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "",
    "author": "Frazer McLean",
    "author_email": "frazer@frazermclean.co.uk",
    "download_url": "https://files.pythonhosted.org/packages/5b/e0/b36010bddcf91444ff51179c076e4a09c513674a56758d7cfea4f6520e29/ratelimiter-1.2.0.post0.tar.gz",
    "platform": "",
    "description": "RateLimiter\n===========\n\n|PyPI Version| |Build Status| |Python Version| |License|\n\nSimple Python module providing rate limiting.\n\nOverview\n--------\n\nThis package provides the ``ratelimiter`` module, which ensures that an\noperation will not be executed more than a given number of times on a\ngiven period. This can prove useful when working with third parties APIs\nwhich require for example a maximum of 10 requests per second.\n\nUsage\n-----\n\nDecorator\n~~~~~~~~~\n\n.. code:: python\n\n    from ratelimiter import RateLimiter\n\n    @RateLimiter(max_calls=10, period=1)\n    def do_something():\n        pass\n\nContext Manager\n~~~~~~~~~~~~~~~\n\n.. code:: python\n\n    from ratelimiter import RateLimiter\n\n    rate_limiter = RateLimiter(max_calls=10, period=1)\n\n    for i in range(100):\n        with rate_limiter:\n            do_something()\n\nCallback\n~~~~~~~~\n\nThe callback is called in its own thread, so your callback may use\n``sleep`` without delaying the rate limiter.\n\n.. code:: python\n\n    import time\n\n    from ratelimiter import RateLimiter\n\n    def limited(until):\n        duration = int(round(until - time.time()))\n        print('Rate limited, sleeping for {:d} seconds'.format(duration))\n\n    rate_limiter = RateLimiter(max_calls=2, period=3, callback=limited)\n\n    for i in range(3):\n        with rate_limiter:\n            print('Iteration', i)\n\nOutput:\n\n::\n\n    Iteration 0\n    Iteration 1\n    Rate limited, sleeping for 3 seconds\n    Iteration 2\n\nasyncio\n~~~~~~~\n\nThe ``RateLimiter`` object can be used in an ``async with`` statement on\nPython 3.5+. Note that the callback must be a coroutine in this context.\nThe coroutine callback is not called in a separate thread.\n\n.. code:: python\n\n    import asyncio\n    import time\n\n    from ratelimiter import RateLimiter\n\n    async def limited(until):\n        duration = int(round(until - time.time()))\n        print('Rate limited, sleeping for {:d} seconds'.format(duration))\n\n    async def coro():\n        rate_limiter = RateLimiter(max_calls=2, period=3, callback=limited)\n        for i in range(3):\n            async with rate_limiter:\n                print('Iteration', i)\n\n    loop = asyncio.get_event_loop()\n    loop.run_until_complete(coro())\n\nLicense\n-------\n\n| Original work Copyright 2013 Arnaud Porterie\n| Modified work Copyright 2016 Frazer McLean\n\nLicensed under the Apache License, Version 2.0 (the \u201cLicense\u201d); you may\nnot use this file except in compliance with the License. You may obtain\na copy of the License at\n\nhttp://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software\ndistributed under the License is distributed on an \u201cAS IS\u201d BASIS,\nWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\nSee the License for the specific language governing permissions and\nlimitations under the License.\n\n.. |PyPI Version| image:: http://img.shields.io/pypi/v/ratelimiter.svg?style=flat-square\n   :target: https://pypi.python.org/pypi/ratelimiter\n.. |Build Status| image:: http://img.shields.io/travis/RazerM/ratelimiter/master.svg?style=flat-square\n   :target: https://travis-ci.org/RazerM/ratelimiter\n.. |Python Version| image:: https://img.shields.io/badge/python-2.7%2C%203-brightgreen.svg?style=flat-square\n   :target: https://www.python.org/downloads/\n.. |License| image:: http://img.shields.io/badge/license-Apache-blue.svg?style=flat-square\n   :target: https://github.com/RazerM/ratelimiter/blob/master/LICENSE\n\n\n",
    "bugtrack_url": null,
    "license": "Apache",
    "summary": "Simple python rate limiting object",
    "version": "1.2.0.post0",
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "51802164fa1e863ad52cc8d870855fba0fbb51edd943edffd516d54b5f6f8ff8",
                "md5": "1dabe725652d50c53d794263b46c4e28",
                "sha256": "a52be07bc0bb0b3674b4b304550f10c769bbb00fead3072e035904474259809f"
            },
            "downloads": -1,
            "filename": "ratelimiter-1.2.0.post0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "1dabe725652d50c53d794263b46c4e28",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 6642,
            "upload_time": "2017-12-12T00:33:37",
            "upload_time_iso_8601": "2017-12-12T00:33:37.505339Z",
            "url": "https://files.pythonhosted.org/packages/51/80/2164fa1e863ad52cc8d870855fba0fbb51edd943edffd516d54b5f6f8ff8/ratelimiter-1.2.0.post0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "5be0b36010bddcf91444ff51179c076e4a09c513674a56758d7cfea4f6520e29",
                "md5": "5e677cb4049c85d880b6705b84603820",
                "sha256": "5c395dcabdbbde2e5178ef3f89b568a3066454a6ddc223b76473dac22f89b4f7"
            },
            "downloads": -1,
            "filename": "ratelimiter-1.2.0.post0.tar.gz",
            "has_sig": false,
            "md5_digest": "5e677cb4049c85d880b6705b84603820",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 9182,
            "upload_time": "2017-12-12T00:33:38",
            "upload_time_iso_8601": "2017-12-12T00:33:38.783530Z",
            "url": "https://files.pythonhosted.org/packages/5b/e0/b36010bddcf91444ff51179c076e4a09c513674a56758d7cfea4f6520e29/ratelimiter-1.2.0.post0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2017-12-12 00:33:38",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "github_user": "RazerM",
    "github_project": "ratelimiter",
    "travis_ci": true,
    "coveralls": false,
    "github_actions": false,
    "lcname": "ratelimiter"
}
        
Elapsed time: 0.04305s