async wrapper
Project description
async-wrapper
how to install
$ pip install async_wrapper
# or
$ pip install "async_wrapper[all]"
# or
$ pip install "async_wrapper[loky]"
how to use
from __future__ import annotations
import time
import anyio
from async_wrapper import TaskGroupWrapper, async_to_sync
@async_to_sync("thread")
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.4.1.tar.gz
(6.6 kB
view details)
Built Distribution
File details
Details for the file async_wrapper-0.4.1.tar.gz
.
File metadata
- Download URL: async_wrapper-0.4.1.tar.gz
- Upload date:
- Size: 6.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.5.1 CPython/3.10.6 Linux/5.15.0-1041-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8c8f774e36f33fb8b21c42ced2f1f7dbb54ad57c830f08d3e78284baec11d00c |
|
MD5 | 79836db5ac7bf6e6bdd6aad835488290 |
|
BLAKE2b-256 | ec685704007f2c53861314eb7bbbfc5a182f1e37e1ea2a69eca3e5beb94c9a7f |
File details
Details for the file async_wrapper-0.4.1-py3-none-any.whl
.
File metadata
- Download URL: async_wrapper-0.4.1-py3-none-any.whl
- Upload date:
- Size: 8.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.5.1 CPython/3.10.6 Linux/5.15.0-1041-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f3ada1c4d4d3e4f251f37c5d26222e3d61be7aa200f60cf3cd66112f22417844 |
|
MD5 | 0288aa0abc28ad4f13446569caeabd4b |
|
BLAKE2b-256 | 5795bac4d667e96c73c3494fa9fb1604096ff0a6b2db6ca8f399d98e55bc62f2 |