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.Modeas 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)
- 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.Modeas 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:
- Return type:
- for ... in call_sync(*args, **kwargs)¶
Call the generator function in sync mode.
- Parameters:
- Return type:
- async for ... in call_zync(zync_mode, /, *args, **kwargs)¶
Call the generator function in the given mode.
- Parameters:
- Return type:
- class zcontextmanager[**P, ReturnT_co](func)¶
Similar to
@contextlib.contextmanager, but callable in both sync and async modes.The function must take a
zyncio.Modeas 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:
- Return type:
- with call_sync(*args, **kwargs)¶
Enter the context manager in sync mode.
- Parameters:
- Return type:
- async with call_zync(zync_mode, /, *args, **kwargs)¶
Enter the context manager in the given mode.
- Parameters:
- Return type: