Skip to main content

Provide sync compatibility for your async functions

Reason this release was yanked:

Not proper implemented

Project description

Easy Sync

Provide sync compatibility for your async functions

Motivation

I don't want to always duplicate implementations for both synchronous and asynchronous versions of the same thing, and think out two name for each of them, as it is a waste of time.

So I hope there is some magic which can turn my asynchronous function implementations into synchronous versions.

I call this magic @sync_compatible here, with this decorator, your function f can be called via both await f(x) (in a asynchronous context) or f(x).wait() (in a synchronous context).

Features

  1. Use a single name of function for both async and sync version
  2. Automatic provide a sync vertion from async version (WIP)
  3. Lightweight, pure python, and no dependencies
  4. Strict type annotation (validated by pylance the strict mode)
  5. Unit test, and test coverage ratio is monitored

Usage

The Magic Style (WIP)

from easy_sync import sync_compatible

@sync_compatible
async def async_add(a: int, b: int) -> int:
    await asyncio.sleep(1)
    ''' Add two numbers asynchronously '''
    return a + b

def do_sync():

    print(async_add(1, 2).wait())
    print(async_add(3, 4).wait())

do_sync()

async def async_main():
    result = await async_add(1, 2)
    print(result)

asyncio.run(async_main())

NOTE: This usage is WIP and without a proper implementation yet.

I would be extremely grateful if someone could contribute a proper implementation.

Any discussion about it's implementation is welcome here.

There is also a discussion on StackOverflow Question.

The Name Reusing Style

Use this helper to just reuse name only, you need to provide the sync version yourself.

This is not our final goal, but at least it solves the naming problem for now :)

from easy_sync import sync_compatible

@sync_compatible(sync_fn = _sync_add)
async def async_add(a: int, b: int) -> int:
    await asyncio.sleep(1)
    return a + b

def _sync_add(a: int, b: int) -> int:
    sleep(1)
    return a + b

def main():
    print(async_add(1, 2).wait())
    print(async_add(3, 4).wait())

main()

Run tests and Contribute

You can use nix develop . or poetry shell under the project root to enter the develop environment.

Currently, if you run pytest you will find that some cases is marked as xfailed, which are tests for WIP usage.

You can change the alternative impl code to your own implementation and re-run pytest.

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

easy_sync-0.1.0.tar.gz (6.7 kB view details)

Uploaded Source

Built Distribution

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

easy_sync-0.1.0-py3-none-any.whl (7.2 kB view details)

Uploaded Python 3

File details

Details for the file easy_sync-0.1.0.tar.gz.

File metadata

  • Download URL: easy_sync-0.1.0.tar.gz
  • Upload date:
  • Size: 6.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.11.9 Linux/6.9.7-zen1

File hashes

Hashes for easy_sync-0.1.0.tar.gz
Algorithm Hash digest
SHA256 97e08f940e929d93477c2bed07fa075dbc6a485a0f0e3b4800bcf9e27f695d54
MD5 30cd546ea3f10f80a5c76885edf5d800
BLAKE2b-256 f4ed20be19cdde2f4f76d3ce361ad6d2ad930392763179bf25551d459dff22c7

See more details on using hashes here.

File details

Details for the file easy_sync-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: easy_sync-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 7.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.11.9 Linux/6.9.7-zen1

File hashes

Hashes for easy_sync-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ab3135e398fa096bfbf8c93ba1f31b08bf9a1259269404a395a61640bcc7a70a
MD5 91d62c105f6be7583416b24990bba258
BLAKE2b-256 9d001a9a0b1b9c76a0b2412ef2e55e29ef28decee1432d7e0d133c35a690e9c6

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