Skip to main content

async wrapper

Project description

async-wrapper

License: MIT Documentation Status github action codecov PyPI version python version

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


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.11.0.tar.gz (16.1 kB view details)

Uploaded Source

Built Distribution

async_wrapper-0.11.0-py3-none-any.whl (22.0 kB view details)

Uploaded Python 3

File details

Details for the file async_wrapper-0.11.0.tar.gz.

File metadata

  • Download URL: async_wrapper-0.11.0.tar.gz
  • Upload date:
  • Size: 16.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for async_wrapper-0.11.0.tar.gz
Algorithm Hash digest
SHA256 ff5fb8946f4f758318b8ca5d01859e8030842040c3debc148734d9116db86091
MD5 ee639337557a5f41ff918ba145f94e9e
BLAKE2b-256 e39651b04c5ee18f0dc27668f8b127541bde2faaf9ac9c1259a48c8927e108e5

See more details on using hashes here.

File details

Details for the file async_wrapper-0.11.0-py3-none-any.whl.

File metadata

File hashes

Hashes for async_wrapper-0.11.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f85724d2441b038b35e2895e54a73096c8bfb9f681c739a8f8d6dd5f965c75f0
MD5 5830c20f11559267eee3926070982240
BLAKE2b-256 484590c63fca533ad37e0abf00c090ea988184e18bc13fbfc503c98342acbc0a

See more details on using hashes here.

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