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

Uploaded Source

Built Distribution

async_wrapper-0.2.1-py3-none-any.whl (15.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: async_wrapper-0.2.1.tar.gz
  • Upload date:
  • Size: 9.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.2.1.tar.gz
Algorithm Hash digest
SHA256 052157a267c8d1f12453d47016d7cda44b98292d3029048d57dd02c03370ecac
MD5 b4c20a9462794e37fd8947b0a990566e
BLAKE2b-256 f573440d1179333ce4e4c35d90b4afd57eedcc599ebdff5fadcf77373edd44c5

See more details on using hashes here.

File details

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

File metadata

  • Download URL: async_wrapper-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 15.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.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 6e02288a2c3984d3bbb3e10381ca4b43ad7303f646912f124e6585ee9dd853b3
MD5 28898642851066632a99ec68ceede3f7
BLAKE2b-256 ac8a953256a60de0b825c003f89fcb1bf9f527d8f712b6fc796782fc27a67622

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