Skip to main content

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

Project description

Xsync

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.

We'll use reading a text file as an example. Normally, users would have to use different function/method names depending on whether it was sync or async:

read_file("How are you?")
await read_file_async("How are you?")

However, with Xsync, users don't have to worry about that:

read_file("I'm great thanks!")
await read_file("I'm great thanks!")

This provides a slick and dynamic interface which is oftentimes far more intuitive.

Usage

Hybrid interfaces can be created with the help of Xsync's maybe_async decorator:

import aiofiles
import xsync

@xsync.maybe_async()
def read_file(path: str) -> str:
    with open(path) as f:
        return f.read()

async def _async_read_file(path: str) -> str:
    async with aiofiles.open(path) as f:
        return await f.read()

Here, read_file and _async_read_file are sync and async implementations of the same action. The maybe_async decorator would determine which of the two to run at runtime, based on whether the code is running in an async context.

So read_file would be executed when doing the following...

read_file("path/to/file")

...but _async_read_file would be executed when doing the following instead:

await read_file("path/to/file")

The _async_ prefix is important, as this is what Xsync uses to find async implementations.

This also works with methods within classes (as well as classmethods, provided the classmethod decorator is above the maybe_async one):

import xsync

class Reader:
    @xsync.maybe_async()
    def read_file(self, path: str) -> str:
        ...

    async def _async_read_file(self, path: str) -> str:
        ...

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

Uploaded Source

Built Distribution

xsync-0.1.1-py3-none-any.whl (6.3 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for xsync-0.1.1.tar.gz
Algorithm Hash digest
SHA256 0dae550113042960b68caabbc968264129588b2ee33335fbc18abbb262dbcdb2
MD5 ef7a7226528bc5221baf5463607280f6
BLAKE2b-256 b5c7a340efc3fba79a80d943eaae118126dd523983bc582a156414b69f8e26ef

See more details on using hashes here.

Provenance

File details

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

File metadata

  • Download URL: xsync-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 6.3 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.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 33c7ef611925fab8a271139d85bb179b17af786b2922ffbedaa8a774db1ca724
MD5 fd2606e689cf72b10f23650240a07758
BLAKE2b-256 cd9d881724a9efcb7d9f83cac4fa3675bb3418253b0bb9411d28ebfd12d0620e

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