Standalone API

The standalone API lets you write ZyncIO code that’s not tied to a class.

Decorators

class zfunc[**P, ReturnT_co](func)

Wrap a function to be callable in both sync and async mode.

The function must take a zyncio.Mode as its first parameter.

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

zync_sleep.call_sync(1.0)

await zync_sleep.call_async(1.0)
Parameters:

func (async (Mode, *P.args, **P.kwargs) -> ReturnT_co) – The function to wrap.

await call_async(*args, **kwargs)

Call the function in ASYNC mode.

Parameters:
  • args (P.args) – Positional arguments to forward to the wrapped function.

  • kwargs (P.kwargs) – Keyword arguments to forward to the wrapped function.

Return type:

ReturnT_co

call_sync(*args, **kwargs)

Call the function in SYNC mode.

Parameters:
  • args (P.args) – Positional arguments to forward to the wrapped function.

  • kwargs (P.kwargs) – Keyword arguments to forward to the wrapped function.

Return type:

ReturnT_co

await call_zync(zync_mode, /, *args, **kwargs)

Call the function in the given Mode.

Parameters:
  • zync_mode (Mode) – The Mode to use.

  • args (P.args) – Positional arguments to forward to the wrapped function.

  • kwargs (P.kwargs) – Keyword arguments to forward to the wrapped function.

Return type:

ReturnT_co

class zgenerator[**P, ReturnT_co, SendT_contra](func)

Wrap a generator function to be callable in both sync and async mode.

The function must take a zyncio.Mode as its first parameter.

Example
@zyncio.zgenerator
async def countdown(zync_mode: zyncio.Mode, start: int) -> AsyncGenerator[int]:
    for i in range(start, 0, -1):
        await zync_sleep.call_zync(zync_mode, 1.0)
        yield i

for n in countdown.call_sync(5):
    print(n)

async for n in countdown.call_async(5):
    print(n)
Parameters:

func ((Mode, *P.args, **P.kwargs) -> AsyncGenerator[ReturnT_co, SendT_contra]) – The generator function to wrap.

async for ... in call_async(*args, **kwargs)

Call the generator function in async mode.

Parameters:
  • args (P.args) – Positional arguments to forward to the wrapped function.

  • kwargs (P.kwargs) – Keyword arguments to forward to the wrapped function.

Return type:

AsyncGenerator[ReturnT_co, SendT_contra]

for ... in call_sync(*args, **kwargs)

Call the generator function in sync mode.

Parameters:
  • args (P.args) – Positional arguments to forward to the wrapped function.

  • kwargs (P.kwargs) – Keyword arguments to forward to the wrapped function.

Return type:

Generator[ReturnT_co, SendT_contra]

async for ... in call_zync(zync_mode, /, *args, **kwargs)

Call the generator function in the given mode.

Parameters:
  • args (P.args) – Positional arguments to forward to the wrapped function.

  • kwargs (P.kwargs) – Keyword arguments to forward to the wrapped function.

Return type:

AsyncGenerator[ReturnT_co, SendT_contra]

class zcontextmanager[**P, ReturnT_co](func)

Similar to @contextlib.contextmanager, but callable in both sync and async modes.

The function must take a zyncio.Mode as its first parameter.

Example
@zyncio.zcontextmanager
async def run_process(zync_mode: zyncio.Mode, command: str) -> AsyncGenerator[int]:
    if zync_mode is zyncio.SYNC:
        process = subprocess.Popen(command, shell=True)
    else:
        process = await asyncio.create_subprocess_shell(command)

    try:
        yield process.pid
    finally:
        process.terminate()

with run_process.call_sync('some command') as pid:
    print(pid)

async def main() -> None:
    async with run_process.call_async('some command') as pid:
        print(pid)
Parameters:

func ((Mode, *P.args, **P.kwargs) -> AsyncGenerator[ReturnT_co, None]) – The generator function to wrap.

async with call_async(*args, **kwargs)

Enter the context manager in async mode.

Parameters:
  • args (P.args) – Positional arguments to forward to the wrapped function.

  • kwargs (P.kwargs) – Keyword arguments to forward to the wrapped function.

Return type:

AbstractAsyncContextManager[ReturnT_co]

with call_sync(*args, **kwargs)

Enter the context manager in sync mode.

Parameters:
  • args (P.args) – Positional arguments to forward to the wrapped function.

  • kwargs (P.kwargs) – Keyword arguments to forward to the wrapped function.

Return type:

AbstractContextManager[ReturnT_co]

async with call_zync(zync_mode, /, *args, **kwargs)

Enter the context manager in the given mode.

Parameters:
  • zync_mode (Mode) – The Mode to use.

  • args (P.args) – Positional arguments to forward to the wrapped function.

  • kwargs (P.kwargs) – Keyword arguments to forward to the wrapped function.

Return type:

AbstractAsyncContextManager[ReturnT_co]