Skip to main content

Python decorator for async properties.

Project description

async_property Documentation Status Updates

Python decorator for async properties.


To install async_property, run this command in your terminal:

$ pip install async-property

Or if you have pipenv:

$ pipenv install async-property


You can use @async_property just as you would with @property, but on an async function.

class Foo:
    async def remote_value(self):
        return await get_remote_value()

The property remote_value now returns an awaitable coroutine.

instance = Foo()
await instance.remote_value

Cached Properties

@async_cached_property will call the function only once. Subsequent awaits to the property will return a cached value.

class Foo:
    async def value(self):
        print('loading value')
        return 123

>>> instance = Foo()
>>> instance.value
<AwaitableOnly "Foo.value">

>>> await instance.value
loading value
>>> await instance.value
>>> instance.value

>>> instance.value = 'abc'
>>> instance.value
>>> await instance.value

>>> del instance.value
>>> await instance.value
loading value


If you have an object with multiple cached properties, you can subclass AwaitLoader. This will make your class instances awaitable and will load all @async_cached_property fields concurrently. AwaitLoader will call await instance.load(), if it exists, before loading properties.

class Foo(AwaitLoader):
    async def load(self):
        print('load called')

    async def db_lookup(self):
        return 'success'

    async def api_call(self):
        print('calling api')
        return 'works every time'

>>> instance = await Foo()
load called
calling api
>>> instance.db_lookup
>>> instance.api_call
'works every time'


  • Both regular and cached property.

  • Cached properties can be accessed multiple times without repeating function call.

  • Uses asyncio.Lock to ensure cached functions are called only once.

  • Full test coverage with py.test


This package was created with Cookiecutter and the audreyr/cookiecutter-pypackage project template.

The ObjectProxy class was taken from wrapt library by Graham Dumpleton.


0.2.2 (2023-07-03)

  • Add Python 3.11 support and drop Python 3.6.

0.2.1 (2019-04-13)

  • Update docs and readme

0.2.0 (2019-04-12)

  • Use instance state to hold cache and locks

0.1.4 (2019-04-12)

  • Fix inheritance issues on AwaitLoader

0.1.3 (2019-04-12)

  • Cleanup code

0.1.2 (2019-04-12)

  • Fix asyncio.Lock issues

0.1.1 (2019-04-11)

  • Complete test coverage and update readme

0.1.0 (2019-04-11)

  • First release on PyPI.

Project details

Download files

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

Source Distribution

async_property-0.2.2.tar.gz (16.5 kB view hashes)

Uploaded Source

Built Distribution

async_property-0.2.2-py2.py3-none-any.whl (9.5 kB view hashes)

Uploaded Python 2 Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page