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.7.1.tar.gz
(15.5 kB
view details)
Built Distribution
File details
Details for the file async_wrapper-0.7.1.tar.gz
.
File metadata
- Download URL: async_wrapper-0.7.1.tar.gz
- Upload date:
- Size: 15.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.7.1 CPython/3.8.18 Linux/6.2.0-1019-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a3a17938dc36b071c803f8671a6dd7a275bdccb60002894976109a08e9b68904 |
|
MD5 | 645596aca5bffef93d07a4fc6d1ba2c7 |
|
BLAKE2b-256 | fc44fdc3d00f5a7999524667dc370dd71b0ba96fda61b9aa974f2fed2c29ab06 |
File details
Details for the file async_wrapper-0.7.1-py3-none-any.whl
.
File metadata
- Download URL: async_wrapper-0.7.1-py3-none-any.whl
- Upload date:
- Size: 18.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.7.1 CPython/3.8.18 Linux/6.2.0-1019-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0d78de81869970734eab976ffbb791ca997dc9092d4d18078d5acbaa5d9813df |
|
MD5 | 664a852aadbcc5e120c0704d54275a02 |
|
BLAKE2b-256 | 0321f8395a34f862dd8a24fea5c7a7ecdad98d4de801801d5ab2c453877971b7 |