https://github.com/ryananguiano/async_property
Python decorator for async properties.
https://github.com/ryananguiano/async_property
Last synced: 18 days ago
JSON representation
Python decorator for async properties.
- Host: GitHub
- URL: https://github.com/ryananguiano/async_property
- Owner: ryananguiano
- License: mit
- Created: 2019-04-11T07:47:00.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2023-09-05T05:31:37.000Z (over 1 year ago)
- Last Synced: 2025-04-10T22:41:41.166Z (about 2 months ago)
- Language: Python
- Homepage:
- Size: 111 KB
- Stars: 91
- Watchers: 1
- Forks: 8
- Open Issues: 9
-
Metadata Files:
- Readme: README.rst
- Changelog: HISTORY.rst
- Contributing: CONTRIBUTING.rst
- License: LICENSE
Awesome Lists containing this project
- awesome-asyncio - async_property - Python decorator for async properties. (Misc)
README
==============
async_property
==============.. image:: https://img.shields.io/pypi/v/async_property.svg
:target: https://pypi.org/project/async-property/.. image:: https://anaconda.org/ryananguiano/async-property/badges/version.svg
:target: https://anaconda.org/ryananguiano/async-property.. image:: https://app.travis-ci.com/ryananguiano/async_property.svg?branch=master
:target: https://app.travis-ci.com/github/ryananguiano/async_property.. image:: https://readthedocs.org/projects/async-property/badge/?version=latest
:target: https://async-property.readthedocs.io/en/latest/?badge=latest
:alt: Documentation Status.. image:: https://pyup.io/repos/github/ryananguiano/async_property/shield.svg
:target: https://pyup.io/repos/github/ryananguiano/async_property/
:alt: UpdatesPython decorator for async properties.
* Python: 3.7+
* Free software: MIT license
* Documentation: https://async-property.readthedocs.io
* Package: https://pypi.org/project/async-property
* Source code: https://github.com/ryananguiano/async_propertyInstall
-------To install async_property, run this command in your terminal:
.. code-block:: console
$ pip install async-property
Or if you have pipenv:
.. code-block:: console
$ pipenv install async-property
Or alternatively with conda:
.. code-block:: console
$ conda install -c ryananguiano async-property
Usage
-----You can use ``@async_property`` just as you would with ``@property``, but on an async function.
.. code-block:: python
class Foo:
@async_property
async def remote_value(self):
return await get_remote_value()The property ``remote_value`` now returns an awaitable coroutine.
.. code-block:: python
instance = Foo()
await instance.remote_valueCached Properties
~~~~~~~~~~~~~~~~~``@async_cached_property`` will call the function only once. Subsequent awaits to the property will return a cached value.
.. code-block:: python
class Foo:
@async_cached_property
async def value(self):
print('loading value')
return 123>>> instance = Foo()
>>> instance.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
123AwaitLoader
~~~~~~~~~~~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.
.. code-block:: python
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.testCredits
-------This package was created with Cookiecutter_ and the `audreyr/cookiecutter-pypackage`_ project template.
.. _Cookiecutter: https://github.com/audreyr/cookiecutter
.. _`audreyr/cookiecutter-pypackage`: https://github.com/audreyr/cookiecutter-pypackageThe ObjectProxy_ class was taken from wrapt_ library by Graham Dumpleton.
.. _ObjectProxy: https://github.com/GrahamDumpleton/wrapt/blob/master/src/wrapt/wrappers.py
.. _wrapt: https://github.com/GrahamDumpleton/wrapt