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

Uploaded Source

Built Distribution

async_wrapper-0.1.5-py3-none-any.whl (14.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: async_wrapper-0.1.5.tar.gz
  • Upload date:
  • Size: 8.7 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.5.tar.gz
Algorithm Hash digest
SHA256 ea7e25a49cc8c1c2d9f81f4d2b30bd8b25f368e55cc8514368987e161cbcf88b
MD5 0ebccc24bc0a014797b1d23c1a34fb3e
BLAKE2b-256 576404be8b74c69e15d8d874df2dbfe28fa0ce8a067a7e521df4a79ee1886100

See more details on using hashes here.

File details

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

File metadata

  • Download URL: async_wrapper-0.1.5-py3-none-any.whl
  • Upload date:
  • Size: 14.8 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.5-py3-none-any.whl
Algorithm Hash digest
SHA256 bca7ab14a86870a0d6c59e695204bbc7e91b1792a486dbab19be86ef759fe2cb
MD5 8cdea850866a686bd601d972ec5d4a47
BLAKE2b-256 0130d0506bc6826472b403ecd51e1db2b7a28c65ab9b18ff82179006052cd3ff

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