Skip to main content

async wrapper

Project description

async-wrapper

License: MIT github action PyPI version python version

how to install

$ 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

from __future__ import annotations

import asyncio

# or(>=py311)
# from asyncio.taskgroups import TaskGroup
from async_wrapper import async_to_sync, 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")
    async with factory() as task_group:
        value_1 = wrapper(sample_func_2, task_group)(1)
        value_2 = wrapper(sample_func_2, task_group)(2)

    assert isinstance(value_1.value, int)
    assert isinstance(value_2.value, int)
    assert value_1.value == 1
    assert value_2.value == 2

License

MIT, see LICENSE.

Project details


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.1.3.tar.gz (7.5 kB view hashes)

Uploaded Source

Built Distribution

async_wrapper-0.1.3-py3-none-any.whl (13.2 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page