Skip to main content

async_property

https://img.shields.io/pypi/v/async_property.svg https://app.travis-ci.com/ryananguiano/async_property.svg?branch=master Documentation Status Updates

Python decorator for async properties.

Install

To install async_property, run this command in your terminal:

$ pip install async-property

Or if you have pipenv:

$ pipenv install async-property

Usage

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

class Foo:
    @async_property
    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_cached_property
    async def value(self):
        print('loading value')
        return 123

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

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

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

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

AwaitLoader

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_cached_property
    async def db_lookup(self):
        return 'success'

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

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

Features

  • 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

Credits

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

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

History

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.

Release files for async-property 0.2.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for async-property 0.2.2
File Size Uploaded
async_property-0.2.2.tar.gz 16.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for async-property 0.2.2
File Interpreter ABI Platform
async_property-0.2.2-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 26.1 kB

Release files / async_property-0.2.2.tar.gz

Download URL async_property-0.2.2.tar.gz
Size 16.5 kB
Tags Source
SHA-256 checksum
How to use checksums
17d9bd6ca67e27915a75d92549df64b5c7174e9dc806b30a3934dc4ff0506380
BLAKE2b-256 checksum
How to use checksums
a712900eb34b3af75c11b69d6b78b74ec0fd1ba489376eceb3785f787d1a0a1d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.0

Release files / async_property-0.2.2-py2.py3-none-any.whl

Download URL async_property-0.2.2-py2.py3-none-any.whl
Size 9.5 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
8924d792b5843994537f8ed411165700b27b2bd966cefc4daeefc1253442a9d7
BLAKE2b-256 checksum
How to use checksums
c7809f608d13b4b3afcebd1dd13baf9551c95fc424d6390e4b1cfd7b1810cd06
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.0

Release history Release notifications | RSS feed

This release

0.2.2 This release

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page