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 asyncio
import time
from async_wrapper import (
async_to_sync,
get_semaphore_class,
get_task_group_factory,
get_task_group_wrapper,
)
@async_to_sync("thread")
async def sample_func() -> int:
await asyncio.sleep(1)
return 1
result = sample_func()
assert isinstance(result, int)
assert result == 1
async def sample_func_2(x: int) -> int:
await asyncio.sleep(1)
return x
async def main():
wrapper = get_task_group_wrapper()
factory = get_task_group_factory()
Semaphore = get_semaphore_class()
semaphore = Semaphore(2)
start = time.perf_counter()
async with factory() as task_group:
wrapped = wrapper(sample_func_2, task_group, semaphore)
value_1 = wrapped(1)
value_2 = wrapped(2)
value_3 = wrapped(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
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.2.2.tar.gz
(9.2 kB
view details)
Built Distribution
File details
Details for the file async_wrapper-0.2.2.tar.gz
.
File metadata
- Download URL: async_wrapper-0.2.2.tar.gz
- Upload date:
- Size: 9.2 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 | 67544e089ab5d133ea289a698f917fea4b053c7c9adeecaeb2fb48025828d0a4 |
|
MD5 | 54388e58c64753f93f9b475f826be3b5 |
|
BLAKE2b-256 | c0143ebcda3e1036820b83328d372577e577c557ec272a465e7672c0a901ac06 |
File details
Details for the file async_wrapper-0.2.2-py3-none-any.whl
.
File metadata
- Download URL: async_wrapper-0.2.2-py3-none-any.whl
- Upload date:
- Size: 14.2 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 | b18ff24200db30f5bde2921411820dd11221ce20607e7a90b38e3fbd1b6f5d95 |
|
MD5 | e5c55e4dae38a00bc20a6e63db37a4af |
|
BLAKE2b-256 | 7f804a9b5909c71ee58d376b001b6bed058c34c7eebf273d1a1bb70667ecd8d5 |