Skip to main content

Convert async functions to sync and vice versa.

Project description

Desync

Simple conversions between synchronous and asynchronous functions and function calls. You can use it to wait (really block) for asynchronous function results with sync or create synchronous versions with synced:

    # Assume async_func is asynchronous.
    async_func: Callable[..., Awaitable[T]]

    # Call async_func and wait for its result synchronously:
    result: T = sync(async_func, *args, **kwargs)

    # Create a version which is synchronous without calling it:
    desynced_func: Callable[..., T] = synced(async_func)

You can also wait for synchronous functions via desync or create asynchronous versions with desynced:

    # Assume sync_func is synchronous.
    func: Callable[..., T]

    # Call sync_func and wait for its result in an asynchronous context:
    awaitable: Awaitable[T] = desync(sync_func, *args, **kwargs)
    result: T = await awaitable

    # Create a version which is asynchronous without calling it:
    descyned_func: Callable[..., Awaitable[T]] = desynced(func)

Desynced functions run in parallel (as far as python can run in parallel).

    def wait_some():
        time.sleep(1)
        return 1

    # This should take approx. 1 second:
    assert 10 == sum(
        await asyncio.gather(*(
            desync(wait_some) for i in range(10)
        ))
    )

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

pydesync-0.1.1.tar.gz (7.0 kB view hashes)

Uploaded Source

Built Distribution

pydesync-0.1.1-py3-none-any.whl (7.5 kB view hashes)

Uploaded Python 3

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