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

Uploaded Source

Built Distribution

async_wrapper-0.1.4-py3-none-any.whl (13.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: async_wrapper-0.1.4.tar.gz
  • Upload date:
  • Size: 8.0 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.4.tar.gz
Algorithm Hash digest
SHA256 910da88fe56e08cb867ebed09e1cb7ec1ef9a8ede23f12980576e65b24dd4cbc
MD5 7a9b462e815b8ce6f5a8e555673efb3e
BLAKE2b-256 3d0c86b691fdce503508d4b739c9d33bc4838ba2ad05be45e87a8a432546eb3e

See more details on using hashes here.

File details

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

File metadata

  • Download URL: async_wrapper-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 13.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.4-py3-none-any.whl
Algorithm Hash digest
SHA256 a2566f804909735fbdeae0fd0d823844cf4db05e8247fb79d7d006afe54ffec9
MD5 0f097cf09f717ad0a74cd05a8ab9b18b
BLAKE2b-256 34b5f58ebabb13e714ba6e0c792d86332cb77de26138bfb17e666a1b60db23c1

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