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

Uploaded Source

Built Distribution

async_wrapper-0.8.4-py3-none-any.whl (19.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: async_wrapper-0.8.4.tar.gz
  • Upload date:
  • Size: 26.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.12.3

File hashes

Hashes for async_wrapper-0.8.4.tar.gz
Algorithm Hash digest
SHA256 df861a2ea44e10471523798e5e6b90c2af4ef464608d03eb571af4da5273df4d
MD5 513075b2e6f12a5384d7c2d936375dee
BLAKE2b-256 9f3cc87155bf8b6de2f1851d26b2234c7b7f994e97689653c50cf652575003a5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for async_wrapper-0.8.4-py3-none-any.whl
Algorithm Hash digest
SHA256 de11af81cc39032c87202af60705ab479bb7a997845296e0c3dcee9b8689ee7c
MD5 f56f6dee6116f72faf8a6cb8f41856bc
BLAKE2b-256 8031f3b0cafc8cf93398bebcb36d062d4098c072d1be6c1fd6e4a7ff814bcb2c

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