Skip to main content

universalasync

CI Codecov PyPI version Docs

A library to help automate the creation of universal python libraries

Overview

Have you ever been frustrated that you need to maintain both sync and async versions of your library, even thought their code differs by just async and await? You might have came up to rewriting your code before release or other unreliable solutions.

This library helps you to focus only on the main async implementation of your library: sync one will be created automatically

Via decorating all your public methods, the wrapped functions automatically detect different conditions and run the functions accordingly.

If user uses your library in async context, minimal overhead is added, it just returns the coroutine right away.

Otherwise the library calls the coroutine via various loop methods, like as if you did it manually.

There should be no issues at all, the only limitation is that signals and async subprocesses are supported only when running in the main thread.

Also note that when run from a different os thread, the library will create a new event loop there and run coroutines. If that thread's event loop is already running in another thread, coroutines are run there and the call waits for the result.

This means that resources bound to a certain event loop (like aiohttp.ClientSession) need one instance per event loop.

LoopLocal keeps one value per event loop, and closes each of them on the loop that owns it: when you call close(), or when it is garbage collected.

import aiohttp
from universalasync import LoopLocal, wrap


@wrap
class Client:
    def __init__(self):
        self._sessions = LoopLocal(aiohttp.ClientSession.close)

    async def help(self):
        async with self._sessions.get(aiohttp.ClientSession).get("https://example.com") as response:
            return await response.text()

    def close(self):  # await it in async code
        return self._sessions.close()

For API reference see the docs

Installation

pip install universalasync

Example of usage

import asyncio
import threading

from universalasync import async_to_sync_wraps, wrap


# wrap needed methods one by one
class Client:
    @async_to_sync_wraps
    async def help(self): ...

    @async_to_sync_wraps
    @property
    async def async_property(self): ...


# or wrap whole classes
@wrap
class Client:
    async def help(self): ...

    @property
    async def async_property(self): ...


client = Client()


def sync_call():
    client.help()
    client.async_property


async def async_call():
    await client.help()
    await client.async_property


# works in all cases
sync_call()
asyncio.run(async_call())
threading.Thread(target=sync_call).start()
threading.Thread(target=asyncio.run, args=(async_call(),)).start()

The mode is chosen when a wrapped method is called: outside a running event loop it runs to completion and returns the result. So asyncio.run(client.help()) doesn't work, as client.help() has already run before asyncio.run starts a loop. Await it inside a coroutine instead, like async_call above.

wrap only converts the class it's applied to, so subclasses adding or overriding async members need their own @wrap.

Type checkers see wrapped methods as async, so sync callers get type errors, and a sync helper called inside a running event loop gets a coroutine instead of a result.

Copyright (C) 2021 MrNaif2018

Universal async-sync wrapper initially based on this implementation

Licensed under the MIT license

Metadata

Release files for universalasync 0.6.3.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for universalasync 0.6.3.1
File Size Uploaded Provenance Actions
universalasync-0.6.3.1.tar.gz 14.1 kB With provenance Details for universalasync-0.6.3.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for universalasync 0.6.3.1
File Interpreter ABI Platform Actions
universalasync-0.6.3.1-py3-none-any.whl Python 3 none any Details for universalasync-0.6.3.1-py3-none-any.whl

Total release size: 23.3 kB

Release files / universalasync-0.6.3.1.tar.gz

Download URL universalasync-0.6.3.1.tar.gz
Size 14.1 kB
Tags Source
SHA-256 checksum
How to use checksums
69866f644ae51f1f4a62c0a740a197bac83b8fd711880267994c4bdb28e1d1df
BLAKE2b-256 checksum
How to use checksums
e8f0c5c6f9fa73e90d1c1fb89736e3e29f40759e80b9bbbc854681954c6bc3b7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 6, 2026.

Transparency log

Release files / universalasync-0.6.3.1-py3-none-any.whl

Download URL universalasync-0.6.3.1-py3-none-any.whl
Size 9.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
49425e71e3af35852e69fc46fed00c08137f3faacbffb9e4b31f1d68a3339873
BLAKE2b-256 checksum
How to use checksums
4b5103672e969857eec67a7dfd3d2cbce0b7edba1dcd745756834f7db5131126
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 6, 2026.

Transparency log

Security for universalasync 0.6.3.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for universalasync-0.6.3.1.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 6, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page