Skip to main content

Write dual sync/async interfaces with minimal duplication.

Project description

ZyncIO

Write dual sync/async interfaces with minimal duplication.

PyPI - Version PyPI - Python Version Coverage


What is ZyncIO?

If I had a nickel for every almost identical interface I had to write, I'd have two nickels... which isn't a lot, but it's weird that I had to write it twice.

– Dr. Doofenshmirtz, before discovering ZyncIO.

ZyncIO allows you to write interfaces that can be used synchronously and asynchronously, while avoiding the code duplication this usually entails.

How does it work?

ZyncIO works due to the fact that in Python you can actually run a coroutine without an event loop, as long as your chain of awaits consists exclusively of other coroutines (i.e. no Futures or Tasks):

The behavior of await coroutine is effectively the same as invoking a regular, synchronous Python function.

A Conceptual Overview of asyncio

To run such a coroutine, we simply call send(None), catch the StopIteration, and extract its value:

coro = pure_coroutine_func()
try:
    coro.send(None)
except StopIteration as e:
    ret = e.value

This means that a single async def function can be made to run in both synchronous and asynchronous contexts, as long as we have a way to determine which mode we're currently using:

async def zync_sleep(zync_mode: zyncio.Mode, secs: float) -> None:
    if zync_mode is zyncio.SYNC:
        time.sleep(secs)
    else:
        await asyncio.sleep(secs)

But this isn't very convenient; you need to pass an additional parameter, and running in sync mode is pretty clunky. That's where zyncio.zfunc comes in:

@zyncio.zfunc
async def zync_sleep(zync_mode: zyncio.Mode, secs: float) -> None:
    ...

zync_sleep.run_sync(3)
asyncio.run(zync_sleep.run_async(3))

@zyncio.zfunc
async def sleep_3(zync_mode: zyncio.Mode) -> None:
    await zync_sleep.run_zync(zync_mode, 3)
    # or
    await zync_sleep[zync_mode](3)

The real magic: SyncMixin/AsyncMixin, zyncio.zmethod, and zyncio.zproperty

The real power of ZyncIO comes out when implementing client interfaces:

  1. Implement a single base client, using the zyncio.zmethod and zyncio.zproperty decorators.

  2. Create two subclasses a sync client and an async client, adding the zyncio.SyncMixin and zyncio.AsyncMixin mixins respectively.

  3. All of your zyncio.zmethods magically become sync methods on the sync client and async methods on the async client.

    All of the zyncio.zpropertys magically become properties on the sync client, and async methods on the async client.

class BaseClient:
    def __init__(self, sock: socket.socket) -> None:
        self.sock: socket.socket = sock

    @zyncio.zmethod
    async def send_msg(self, zync_mode: zyncio.Mode, data: bytes) -> None:
        if zync_mode is zyncio.SYNC:
            self.sock.sendall(data)
        else:
            loop = asyncio.get_running_loop()
            await loop.sock_sendall(self.sock, data)

    @zyncio.zmethod
    async def recv_msg(self, zync_mode: zyncio.Mode, n: int) -> bytes:
        buf = b''
        if zync_mode is zyncio.SYNC:
            while len(buf) < n:
                buf += self.sock.recv(n)
        else:
            loop = asyncio.get_running_loop()
            while len(buf) < n:
                buf += await loop.sock_recv(self.sock, n)
        return buf

    @zyncio.zmethod
    async def do_handshake(self, zync_mode: zyncio.Mode) -> None:
        await self.send_msg[zync_mode](HANDSHAKE_REQ)
        response = await self.recv_msg[zync_mode](len(HANDSHAKE_RESP))
        if response != HANDSHAKE_RESP:
            raise RuntimeError('Handshake failed')

    @zyncio.zproperty
    async def status(self, zync_mode: zyncio.Mode) -> str:
        await self.send_msg[zync_mode](STATUS_REQ)
        return (await self.recv_msg[zync_mode](STATUS_RESP_LEN)).decode()


class SyncClient(BaseClient, zyncio.SyncMixin):
    pass


class AsyncClient(BaseClient, zyncio.AsyncMixin):
    def __init__(self, sock: socket.socket) -> None:
        super().__init__(sock)
        self.sock.setblocking(False)


sync_client = SyncClient(sock)
sync_client.do_handshake()  # Magically sync!
print('Status:', sync_client.status)  # Sync property


async def use_async_client():
    async_client = AsyncClient(sock)
    await async_client.do_handshake()  # Magically async!
    print('Status:', await sync_client.status())  # Async func

asyncio.run(use_async_client())

Typing

ZyncIO is fully typed, and built specifically for typed projects. If you're getting unexepcted type checking errors, please open an issue.

License

zyncio is distributed under the terms of 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

zyncio-0.5.0.tar.gz (29.1 kB view details)

Uploaded Source

Built Distribution

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

zyncio-0.5.0-py3-none-any.whl (7.0 kB view details)

Uploaded Python 3

File details

Details for the file zyncio-0.5.0.tar.gz.

File metadata

  • Download URL: zyncio-0.5.0.tar.gz
  • Upload date:
  • Size: 29.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for zyncio-0.5.0.tar.gz
Algorithm Hash digest
SHA256 16e94b1c2560d8293914355280e63244e8185f3e08d7bbc6922bde6a66e0f7cd
MD5 f04a4c6df89fe9e5cde8adba4021d5a3
BLAKE2b-256 0ca7ccef6e369a41d10ec0ddd7fa8b925ebe022cb49823bb840b5c4d8c8b07fd

See more details on using hashes here.

Provenance

The following attestation bundles were made for zyncio-0.5.0.tar.gz:

Publisher: release.yml on BenjyWiener/zyncio

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file zyncio-0.5.0-py3-none-any.whl.

File metadata

  • Download URL: zyncio-0.5.0-py3-none-any.whl
  • Upload date:
  • Size: 7.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for zyncio-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d27ea8f6509dd140a873371cb7fd59ab578d282eaa4ac7978a2d72b0d076233d
MD5 99eb8f5d9bcda4b898b68976e2ee1445
BLAKE2b-256 e94207cc7a12d91d9d352113602d909055b8f271f4a26cb17d9931bc3d653a61

See more details on using hashes here.

Provenance

The following attestation bundles were made for zyncio-0.5.0-py3-none-any.whl:

Publisher: release.yml on BenjyWiener/zyncio

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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