async wrapper
Project description
async-wrapper
how to install
$ pip install async_wrapper
how to use
from __future__ import annotations
import time
import anyio
from async_wrapper import TaskGroupWrapper, toggle_func
@toggle_func
async def sample_func() -> int:
await anyio.sleep(1)
return 1
async def sample_func_2(x: int) -> int:
await anyio.sleep(1)
return x
def main():
result = sample_func()
assert isinstance(result, int)
assert result == 1
async def async_main():
semaphore = anyio.Semaphore(2)
start = time.perf_counter()
async with anyio.create_task_group() as task_group:
wrapper = TaskGroupWrapper(task_group)
func = wrapper.wrap(sample_func_2, semaphore)
value_1 = func(1)
value_2 = func(2)
value_3 = func(3)
end = time.perf_counter()
assert isinstance(value_1.value, int)
assert isinstance(value_2.value, int)
assert isinstance(value_3.value, int)
assert value_1.value == 1
assert value_2.value == 2
assert value_3.value == 3
assert 1.5 < end - start < 2.5
if __name__ == "__main__":
main()
anyio.run(async_main)
License
MIT, see LICENSE.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
async_wrapper-0.5.2.tar.gz
(13.4 kB
view details)
Built Distribution
File details
Details for the file async_wrapper-0.5.2.tar.gz
.
File metadata
- Download URL: async_wrapper-0.5.2.tar.gz
- Upload date:
- Size: 13.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.5.1 CPython/3.10.6 Linux/5.15.0-1042-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e5aed2c37a316580322e9089d130b377c27470cd15d525915b9618a85dd1bf25 |
|
MD5 | 3026bbf89f1dc7718c1f7ad5b0da0393 |
|
BLAKE2b-256 | 13f66ac211f0a80ac0848a150a442e36e2db0e9eff44bd6e9136cf97ae3ba02c |
File details
Details for the file async_wrapper-0.5.2-py3-none-any.whl
.
File metadata
- Download URL: async_wrapper-0.5.2-py3-none-any.whl
- Upload date:
- Size: 16.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.5.1 CPython/3.10.6 Linux/5.15.0-1042-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 818060b4d43d5375bb988ce09ae180ef079d8fb1382d05a9dafc61f9f287d70d |
|
MD5 | 9314b5c75db00e00a3685d2f0bde28d3 |
|
BLAKE2b-256 | f781dcb2c057dbc1d9d5a22513e7137feae14aa2aaf09c082e9aa25afaca5704 |