litwisha / async_lru

Simple lru cache for asyncio

Home Page:https://pypi.python.org/pypi/async_lru

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

async_lru

info:Simple lru cache for asyncio
https://travis-ci.org/aio-libs/async_lru.svg?branch=master

Installation

pip install async_lru

Usage

import asyncio

import aiohttp
from async_lru import alru_cache

calls = 0

@alru_cache()
async def download(url):
    global calls

    calls += 1

    async with aiohttp.ClientSession() as session:
        async with session.get(url) as response:
            return await response.text()


async def main():
    coros = [
        download('https://www.python.org/'),
        download('https://www.python.org/'),
        download('https://www.python.org/'),
        download('https://www.python.org/'),
        download('https://www.python.org/'),
        download('https://www.python.org/'),
    ]

    await asyncio.gather(*coros)

    assert calls == 1


loop = asyncio.get_event_loop()

loop.run_until_complete(main())

# closing is optional, but strictly recommended
loop.run_until_complete(download.close())

loop.close()

Python 3.3+ is required

Thanks

The library was donated by Ocean S.A.

Thanks to the company for contribution.

About

Simple lru cache for asyncio

https://pypi.python.org/pypi/async_lru

License:MIT License


Languages

Language:Python 100.0%