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
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.

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

Uploaded Source

Built Distribution

async_wrapper-0.1.6-py3-none-any.whl (14.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: async_wrapper-0.1.6.tar.gz
  • Upload date:
  • Size: 8.8 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.1.6.tar.gz
Algorithm Hash digest
SHA256 6e7719e408762b9c10c8f830dbb1ff3aa552efbcda16910d1bc62cc04baaacbd
MD5 7cefc0c11fbf5c386444ca07c0227477
BLAKE2b-256 07c3fe35f7412de7011bfdae7137185de76befb20175f960ca6069f57cc85ac8

See more details on using hashes here.

File details

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

File metadata

  • Download URL: async_wrapper-0.1.6-py3-none-any.whl
  • Upload date:
  • Size: 14.9 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.1.6-py3-none-any.whl
Algorithm Hash digest
SHA256 2d60e1f346a0c78d1fb655333864609bfd9c8edf61e0ce3598923fc11fe31cc3
MD5 d3dfd9dbb5835539de1bd1d2a5c0c364
BLAKE2b-256 876573422dc011f8aa38a503a3740da5ee32ff74d3579f3720a831f398a595ab

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