Skip to main content

A set of tools to create hybrid sync/async interfaces.

Project description

Xsync

PyPi version PyPI - Status Downloads GitHub last commit License

CI Maintainability Test Coverage

A set of tools to create hybrid sync/async interfaces.

CPython versions 3.7 through 3.11-dev and PyPy versions 3.7 through 3.9 are officially supported.

Windows, MacOS, and Linux are all supported.

What does Xsync do?

Xsync allows developers to create dynamic interfaces which can be run in sync or async contexts.

In simple terms, it makes this possible:

result = my_function()
result = await my_function()

How neat is that?!

Usage

The above behaviour can be achieved with the as_hybrid decorator:

@xsync.as_hybrid()
def my_function():
    ...

This sets my_function up as a "hybrid callable", which is capable of being run both synchronously and asynchronously. However, we also need to set an async implementation for my_function for it to work. We can do this using the set_async_impl decorator:

@xsync.set_async_impl(my_function)
async def my_async_function():
    ...

Doing this tells Xsync to run this function instead of my_function when awaiting:

my_function()        # runs as normal
await my_function()  # calls `my_async_function` instead

Don't worry, my_async_function can still be run directly, as you'd expect.

It also works on methods, class methods, and static methods:

class MyClass:
    @xsync.as_hybrid()
    def my_method(self):
        ...

    @xsync.set_async_impl(my_method)
    async def my_async_method(self):
        ...

    @classmethod
    @xsync.as_hybrid()
    def my_class_method(cls):
        ...

    @classmethod
    @xsync.set_async_impl(my_class_method)
    async def my_async_class_method(cls):
        ...

    @staticmethod
    @xsync.as_hybrid()
    def my_static_method(cls):
        ...

    @staticmethod
    @xsync.set_async_impl(my_static_method)
    async def my_async_static_method(cls):
        ...

The above is the newer (and better) of two available implementations.

View the old implementation

The above behaviour can be achieved with the maybe_async decorator:

@xsync.maybe_async()
def my_function():
    ...

This sets my_function up as a "hybrid callable", which is capable of being run both synchronously and asynchronously. However, we also need to set an async implementation for my_function for it to work. We can do this by creating another function of the same name, but with an _async_ prefix:

async def _async_my_function():
    ...

Xsync searches for a function with the name of the original function prefixed by _async_ at runtime, and runs this instead when awaiting:

my_function()        # runs as normal
await my_function()  # calls `_async_my_function` instead

It also works on methods and class methods:

class MyClass:
    @xsync.maybe_async()
    def my_method(self):
        ...

    async def _async_my_method(self):
        ...

    @classmethod
    @xsync.maybe_async()
    def my_class_method(cls):
        ...

    @classmethod
    async def _async_my_class_method(cls):
        ...

This implementation does not work with static methods.

Contributing

Contributions are very much welcome! To get started:

License

The Xsync module for Python is licensed under the BSD 3-Clause 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

xsync-0.2.0.tar.gz (8.2 kB view details)

Uploaded Source

Built Distribution

xsync-0.2.0-py3-none-any.whl (12.0 kB view details)

Uploaded Python 3

File details

Details for the file xsync-0.2.0.tar.gz.

File metadata

  • Download URL: xsync-0.2.0.tar.gz
  • Upload date:
  • Size: 8.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.9.12

File hashes

Hashes for xsync-0.2.0.tar.gz
Algorithm Hash digest
SHA256 09a8f9e2161e29906988c3cfee0e29881e9f93a6cd51cac6a74ce563da49b817
MD5 b72c05d431937826cadd872faed5db5b
BLAKE2b-256 27bb154928be07483589f6b16ee73caaa6f1a16563d19beefb4719459d182e64

See more details on using hashes here.

Provenance

File details

Details for the file xsync-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: xsync-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 12.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.9.12

File hashes

Hashes for xsync-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 65ed9eb08fbe38beb02ae03efb878ba136e776594ebd5d79bb337732327cac31
MD5 0f8e6cc50eb6fc50f8f4f8ce5e6df8c3
BLAKE2b-256 13b6267aca124e28ad04fbf292b7ca7be3a3c80914cc270fe726034ce6bd94c7

See more details on using hashes here.

Provenance

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