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:
- Familiarise yourself with the code of conduct
- Have a look at the contributing guide
License
The Xsync module for Python is licensed under the BSD 3-Clause License.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file xsync-0.1.0.tar.gz
.
File metadata
- Download URL: xsync-0.1.0.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
Algorithm | Hash digest | |
---|---|---|
SHA256 | b1594e1a004cc58b79f3fc16638076cbfb47a78edb712e09637805e9b9cba66f |
|
MD5 | fea29426dd5d9f2cdcfce9339d7b4455 |
|
BLAKE2b-256 | ef903816f3ec155be4e1676130a77903653cca979bccb30d4a7231b740bf1857 |
Provenance
File details
Details for the file xsync-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: xsync-0.1.0-py3-none-any.whl
- Upload date:
- Size: 6.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.0 CPython/3.9.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c69c1f7a82c3911d725ccd62c208a4767a9a6771f4342d69987f03b7868b5547 |
|
MD5 | e6f1ad9f5176943909fb3c6c6ba36344 |
|
BLAKE2b-256 | c36073fa5c71155942cc6ff924bdfaafc9397ae6b4442dee194899099ce3cf11 |