asyncio-throttle


Nameasyncio-throttle JSON
Version 1.0.2 PyPI version JSON
download
home_pagehttps://github.com/hallazzang/asyncio-throttle
SummarySimple, easy-to-use throttler for asyncio
upload_time2021-04-07 13:37:50
maintainer
docs_urlNone
authorHanjun Kim
requires_python>=3.6
licenseMIT
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI
coveralls test coverage No coveralls.
            # asyncio-throttle

[![travis-ci](https://travis-ci.org/hallazzang/asyncio-throttle.svg?branch=master)](https://travis-ci.org/hallazzang/asyncio-throttle)
[![pypi-version](https://badge.fury.io/py/asyncio-throttle.svg)](https://badge.fury.io/py/asyncio-throttle)

Simple, easy-to-use throttler for asyncio.

## Example

```python
import time
import random
import asyncio

from asyncio_throttle import Throttler

async def worker(no, throttler, n):
    for _ in range(n):
        await asyncio.sleep(random.random() * 2)

        async with throttler:
            print(time.time(), 'Worker #%d: Bang!' % no)

async def main():
    throttler = Throttler(rate_limit=5)

    tasks = [
        loop.create_task(worker(no, throttler, 10))
            for no in range(5)
    ]
    await asyncio.wait(tasks)

loop = asyncio.get_event_loop()
loop.run_until_complete(main())
loop.close()
```

Here I limited work rate to 5/sec while there are 5 workers.
And the result:

```plain
1508273760.3462772 Worker #2: Bang!
1508273760.590009 Worker #3: Bang!
1508273760.856431 Worker #0: Bang!
1508273761.0110679 Worker #2: Bang!
1508273761.086856 Worker #4: Bang!
1508273761.350699 Worker #3: Bang!
1508273761.5906 Worker #1: Bang!
1508273761.8655958 Worker #4: Bang!
1508273762.224158 Worker #0: Bang!
1508273762.600234 Worker #2: Bang!
1508273762.694332 Worker #2: Bang!
1508273762.726774 Worker #0: Bang!
1508273762.944273 Worker #4: Bang!
```

## Installation

```bash
$ pip install asyncio-throttle
```

It requires Python 3.6 or later.

## Usage

`asyncio_throttle.Throttler` introduces simple APIs: `flush()` and
`acquire()`. But you will not be interested in those because you can
just use it within `with` statement and it looks nicer.

First, create a throttler given desired rate limit. For example if you
want to limit rate to 500/min, you can make it as:

```python
from asyncio_throttle import Throttler

throttler = Throttler(rate_limit=500, period=60)
```

Then whenever you want to do some jobs which should have limited
rate(e.g. sending request to server), Put it in `async with` statement:

```python
async with throttler:
    send_a_request()
```

It's that easy. `asyncio_throttler` can be easily integrated with
`aiohttp` too:

```python
async def worker(throttler, session):
    while True:
        async with throttler:
            async with session.get('http://example.com') as resp:
                do_some_job_with(await resp.text())

        await asyncio.sleep(0.05)
```



            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/hallazzang/asyncio-throttle",
    "name": "asyncio-throttle",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.6",
    "maintainer_email": "",
    "keywords": "",
    "author": "Hanjun Kim",
    "author_email": "hallazzang@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/13/6f/0e2d42c0e95d50edf63147b8a742703061945e02760f25d6a0e8f028ccb0/asyncio-throttle-1.0.2.tar.gz",
    "platform": "any",
    "description": "# asyncio-throttle\n\n[![travis-ci](https://travis-ci.org/hallazzang/asyncio-throttle.svg?branch=master)](https://travis-ci.org/hallazzang/asyncio-throttle)\n[![pypi-version](https://badge.fury.io/py/asyncio-throttle.svg)](https://badge.fury.io/py/asyncio-throttle)\n\nSimple, easy-to-use throttler for asyncio.\n\n## Example\n\n```python\nimport time\nimport random\nimport asyncio\n\nfrom asyncio_throttle import Throttler\n\nasync def worker(no, throttler, n):\n    for _ in range(n):\n        await asyncio.sleep(random.random() * 2)\n\n        async with throttler:\n            print(time.time(), 'Worker #%d: Bang!' % no)\n\nasync def main():\n    throttler = Throttler(rate_limit=5)\n\n    tasks = [\n        loop.create_task(worker(no, throttler, 10))\n            for no in range(5)\n    ]\n    await asyncio.wait(tasks)\n\nloop = asyncio.get_event_loop()\nloop.run_until_complete(main())\nloop.close()\n```\n\nHere I limited work rate to 5/sec while there are 5 workers.\nAnd the result:\n\n```plain\n1508273760.3462772 Worker #2: Bang!\n1508273760.590009 Worker #3: Bang!\n1508273760.856431 Worker #0: Bang!\n1508273761.0110679 Worker #2: Bang!\n1508273761.086856 Worker #4: Bang!\n1508273761.350699 Worker #3: Bang!\n1508273761.5906 Worker #1: Bang!\n1508273761.8655958 Worker #4: Bang!\n1508273762.224158 Worker #0: Bang!\n1508273762.600234 Worker #2: Bang!\n1508273762.694332 Worker #2: Bang!\n1508273762.726774 Worker #0: Bang!\n1508273762.944273 Worker #4: Bang!\n```\n\n## Installation\n\n```bash\n$ pip install asyncio-throttle\n```\n\nIt requires Python 3.6 or later.\n\n## Usage\n\n`asyncio_throttle.Throttler` introduces simple APIs: `flush()` and\n`acquire()`. But you will not be interested in those because you can\njust use it within `with` statement and it looks nicer.\n\nFirst, create a throttler given desired rate limit. For example if you\nwant to limit rate to 500/min, you can make it as:\n\n```python\nfrom asyncio_throttle import Throttler\n\nthrottler = Throttler(rate_limit=500, period=60)\n```\n\nThen whenever you want to do some jobs which should have limited\nrate(e.g. sending request to server), Put it in `async with` statement:\n\n```python\nasync with throttler:\n    send_a_request()\n```\n\nIt's that easy. `asyncio_throttler` can be easily integrated with\n`aiohttp` too:\n\n```python\nasync def worker(throttler, session):\n    while True:\n        async with throttler:\n            async with session.get('http://example.com') as resp:\n                do_some_job_with(await resp.text())\n\n        await asyncio.sleep(0.05)\n```\n\n\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Simple, easy-to-use throttler for asyncio",
    "version": "1.0.2",
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "md5": "e770314e733bded65133705a4adb00dd",
                "sha256": "4d4c1eb3250f735f59ce842d8d92cd2927c008bd52008797ba030b5787c41f3b"
            },
            "downloads": -1,
            "filename": "asyncio_throttle-1.0.2-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "e770314e733bded65133705a4adb00dd",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.6",
            "size": 4079,
            "upload_time": "2021-04-07T13:37:48",
            "upload_time_iso_8601": "2021-04-07T13:37:48.630076Z",
            "url": "https://files.pythonhosted.org/packages/f6/6a/d18c93722fb56dc1ffed5fb7e7fcff4f8031f80f84c5cc0427363a036b0c/asyncio_throttle-1.0.2-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "md5": "831534e16334514cdbd1b729b6bbba93",
                "sha256": "2675282e99d9129ecc446f917e174bc205c65e36c602aa18603b4948567fcbd4"
            },
            "downloads": -1,
            "filename": "asyncio-throttle-1.0.2.tar.gz",
            "has_sig": false,
            "md5_digest": "831534e16334514cdbd1b729b6bbba93",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.6",
            "size": 3775,
            "upload_time": "2021-04-07T13:37:50",
            "upload_time_iso_8601": "2021-04-07T13:37:50.114785Z",
            "url": "https://files.pythonhosted.org/packages/13/6f/0e2d42c0e95d50edf63147b8a742703061945e02760f25d6a0e8f028ccb0/asyncio-throttle-1.0.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2021-04-07 13:37:50",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "github_user": "hallazzang",
    "github_project": "asyncio-throttle",
    "travis_ci": true,
    "coveralls": false,
    "github_actions": false,
    "lcname": "asyncio-throttle"
}
        
Elapsed time: 0.02604s