Metadata-Version: 2.1
Name: async-wrapper
Version: 0.1.4
Summary: async wrapper
Home-page: https://github.com/phi-friday/async-wrapper
License: MIT
Author: phi
Author-email: phi.friday@gmail.com
Requires-Python: >=3.8
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Provides-Extra: all
Provides-Extra: anyio
Provides-Extra: loky
Requires-Dist: aiotools (>=1.6.1,<2.0.0) ; python_version < "3.11"
Requires-Dist: anyio (>=3.7.0,<4.0.0) ; extra == "all" or extra == "anyio"
Requires-Dist: cloudpickle (>=2.2.1,<3.0.0) ; extra == "all" or extra == "loky"
Requires-Dist: loky (>=3.4.0,<4.0.0) ; extra == "all" or extra == "loky"
Requires-Dist: psutil (>=5.9.5,<6.0.0) ; extra == "all" or extra == "loky"
Requires-Dist: typing-extensions (>=4.6.3,<5.0.0)
Project-URL: Repository, https://github.com/phi-friday/async-wrapper
Description-Content-Type: text/markdown

# async-wrapper

[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
[![github action](https://github.com/phi-friday/async-wrapper/actions/workflows/check.yaml/badge.svg?event=push&branch=dev)](#)
[![PyPI version](https://badge.fury.io/py/async-wrapper.svg)](https://badge.fury.io/py/async-wrapper)
[![python version](https://img.shields.io/pypi/pyversions/async_wrapper.svg)](#)

## how to install
```shell
$ pip install async_wrapper
# or
$ pip install "async_wrapper[all]"
# or
$ pip install "async_wrapper[anyio]"
# or
$ pip install "async_wrapper[loky]"
```

## how to use
```python
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("asyncio")
    factory = get_task_group_factory("asyncio")
    Semaphore = get_semaphore_class("asyncio")
    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](https://github.com/phi-friday/async-wrapper/blob/main/LICENSE).

