Python decorator for async properties.
Project description
async_property
Python decorator for async properties.
Free software: MIT license
Documentation: https://async-property.readthedocs.io.
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 only call the function 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
>>> del instance.value
>>> await instance.value
loading value
123
AwaitLoader
If you have multiple cached properties and would like to load them concurrently, you can subclass AwaitLoader. This makes your class instance awaitable and will load all @async_cached_property fields.
class Foo(AwaitLoader):
@async_cached_property
async def db_lookup(self):
return 'success'
@async_cached_property
async def api_call(self):
return 'works every time'
>>> instance = await Foo()
>>> 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.
Cached properties use asyncio.Lock to ensure function is only called once.
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.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
Built Distribution
File details
Details for the file async_property-0.1.3.tar.gz
.
File metadata
- Download URL: async_property-0.1.3.tar.gz
- Upload date:
- Size: 14.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 13ce162d62f9d3825f4b240a4d5e771b8fc01475d3b6f4ea4811a58d11812028 |
|
MD5 | 32b6f431d17923e9481001156e06688c |
|
BLAKE2b-256 | dc99f2db0ce77c03117f051011d37dde4c59e68c0d8d1a6b34173b266b227228 |
File details
Details for the file async_property-0.1.3-py2.py3-none-any.whl
.
File metadata
- Download URL: async_property-0.1.3-py2.py3-none-any.whl
- Upload date:
- Size: 8.9 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 03cf40019c174716033ded91dae72d8f1663890ee80929495798d99e4725d32e |
|
MD5 | 0e208c3e0661b85ebab509c82c657b57 |
|
BLAKE2b-256 | b5e23e3cac06ee905621605e4a1412efdb625e292a1bb6e8034fbe0c2af12c25 |