Skip to main content

A utility for creating synchronous and asynchronous compatible methods.

Project description

syncra

?? Syncra provides async harmony. See the examples below to learn how to create functions and classes that work in both synchronous and asynchronous contexts.

Features

  • Async and Sync Compatibility: Easily create functions that work in both synchronous and asynchronous contexts.
  • Flexible Initialization: Use AsyncObj for classes requiring asynchronous initialization.
  • Decorator for Async Methods: Simplify async method usage with the sync_compat decorator.

Installation

Install via pip:

pip install syncra

Usage

sync_async_factory

Create a function that can be called synchronously or asynchronously:

from syncra import sync_async_factory
import asyncio

def sync_func(x):
    return x * 2


async def async_func(x):
    await asyncio.sleep(0.1)
    return x * 2


syncra = sync_async_factory(sync_func, async_func)

# Usage:
result = syncra(5)  # Synchronous


async def main():  # Asynchronous
    result = await syncra(5)

@sync_compat Decorator

Make an async function compatible with sync calls

from syncra import sync_compat
import asyncio


@sync_compat
async def async_function(x):
    await asyncio.sleep(0.1)
    return x * 2


# Usage:
result = async_function(5)  # Synchronous


async def main():  # Asynchronous
    result = await async_function(5)

AsyncObj Class

Create a class that can be initialized asynchronously:

from syncra import AsyncObj


class MyAsyncClass(AsyncObj):
    async def __ainit__(self, x):
        self.x = x


# Usage:
async def main():
    obj = await MyAsyncClass(10)
    print(obj.x)

Licence

This project is 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

syncra-1.0.3.tar.gz (4.4 kB view details)

Uploaded Source

Built Distribution

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

syncra-1.0.3-py3-none-any.whl (4.8 kB view details)

Uploaded Python 3

File details

Details for the file syncra-1.0.3.tar.gz.

File metadata

  • Download URL: syncra-1.0.3.tar.gz
  • Upload date:
  • Size: 4.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.5

File hashes

Hashes for syncra-1.0.3.tar.gz
Algorithm Hash digest
SHA256 aec5b3ab0332e45ed7ae44aa7338566155695c9f1cfb1609165850a3aa3fa126
MD5 bf02c73002c90295a73bfdcf3c248f60
BLAKE2b-256 984a07990e3ec96ceecb11e071b7c4014ea4d4e6bdb1cd9e03b61f3f5da33370

See more details on using hashes here.

File details

Details for the file syncra-1.0.3-py3-none-any.whl.

File metadata

  • Download URL: syncra-1.0.3-py3-none-any.whl
  • Upload date:
  • Size: 4.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.5

File hashes

Hashes for syncra-1.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 867a5018a3bfd8491125d8e4d483383b16f54210deb07f4551ddca3043821470
MD5 bc9263de608788270e0a8ceb6abcd716
BLAKE2b-256 e5a3d8e59aae7787f4847146760e7de930a966fd4aae5c4aafa2e0cd877db700

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