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[loky]"

how to use

from __future__ import annotations

import time

import anyio

from async_wrapper import TaskGroupWrapper, async_to_sync


@async_to_sync("thread")
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.3.1.tar.gz (9.4 kB view details)

Uploaded Source

Built Distribution

async_wrapper-0.3.1-py3-none-any.whl (14.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: async_wrapper-0.3.1.tar.gz
  • Upload date:
  • Size: 9.4 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

Hashes for async_wrapper-0.3.1.tar.gz
Algorithm Hash digest
SHA256 b98b50f15d84c0920e80c3cafdfa7a0c52718de3a6a8e5680657053e94d399ac
MD5 399a454a84033e6083bcdecb69d8a203
BLAKE2b-256 956f8f25740ef5581a18361c7052f6738f356e8dff55e33c7e40e7fa94d56097

See more details on using hashes here.

File details

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

File metadata

  • Download URL: async_wrapper-0.3.1-py3-none-any.whl
  • Upload date:
  • Size: 14.6 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

Hashes for async_wrapper-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 81ae65b9e01f39657913e1d5b0829566a53aaa1e82c5f675d57fcf6d59b72bb5
MD5 9a6355514e42dc1fc554edfdcf27b186
BLAKE2b-256 9a34d38643c143aa9df9740a2726eb62c0c3e91395097f7c7a317b61754eb8f3

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