Skip to main content

Simple lru_cache for asyncio

Project description

info:Simple lru cache for asyncio
https://img.shields.io/travis/wikibusiness/async_lru.svg https://img.shields.io/pypi/v/async_lru.svg

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for async_lru, version 0.0.6
Filename, size File type Python version Upload date Hashes
Filename, size async_lru-0.0.6.tar.gz (4.1 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page