Skip to main content

A library to help automate the creation of universal python libraries

Project description

universalasync

CircleCI Codecov PyPI version Read the 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.

This means that you might need to adapt your code a bit in case you use some resources bound to a certain event loop (like aiohttp.ClientSession).

You can see an example of how this could be solved here

For API reference see the docs

Installation

pip install universalasync

Example of usage

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

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

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

    @property
    async def async_property():
        ...

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()

Copyright and License

Copyright (C) 2021 MrNaif2018

Universal async-sync wrapper initially based on this implementation

Licensed under the MIT 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

universalasync-0.4.0.0.tar.gz (32.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

universalasync-0.4.0.0-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

Details for the file universalasync-0.4.0.0.tar.gz.

File metadata

  • Download URL: universalasync-0.4.0.0.tar.gz
  • Upload date:
  • Size: 32.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.9

File hashes

Hashes for universalasync-0.4.0.0.tar.gz
Algorithm Hash digest
SHA256 7407f283bb14c0ec70d83672a967de9a06934bc4dd1c768ca817cbdff99c7425
MD5 6d590523ef1eb1fb665118b2caa6e6b9
BLAKE2b-256 b0bcd5be793774d36c1dc75e3b83fbf49a0ea006aee17f568788de1a8d4b9f2b

See more details on using hashes here.

File details

Details for the file universalasync-0.4.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for universalasync-0.4.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5a34333f78a4a121cca2d320529fdcd22e815a7b79dd3b8e530bb3259c01e2aa
MD5 2591c4d38dc0b199d19650d8f4ecf648
BLAKE2b-256 0762eca0bc7eab0a8b52d314710ff2c483d4abf981ea6ecc568453c8ee5bf6bf

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page