Class-Based API¶
The class-based API is the core of ZyncIO.
Decorators and Wrappers¶
- class zmethod[T_co, **P, ReturnT_co](func)¶
Wrap a method to be callable in both sync and async mode.
Decorated methods act like sync methods on
SyncObjects and like async methods onAsyncObjects.Accessing on an instance returns a
BoundZyncMethod.Example
class BaseClient: @zyncio.zmethod async def sleep(self, duration: float) -> None: if zyncio.is_sync(self): time.sleep(duration) else: await asyncio.sleep(duration) class SyncClient(BaseClient, zyncio.SyncMixin): pass class AsyncClient(BaseClient, zyncio.AsyncMixin): pass SyncClient().sleep(1.0) await AsyncClient().sleep(1.0)
- class zclassmethod[T_co, **P, ReturnT_co](func)¶
Wrap a class method to be callable in both sync and async mode.
Decorated methods act like sync
classmethods on subclasses ofSyncMixinand like asyncclassmethods on subclasses ofAsyncMixin.Accessing returns a
BoundZyncClassMethod.Example
class BaseClient: @zyncio.zclassmethod @classmethod async def sleep(cls, duration: float) -> None: if zyncio.is_sync_class(self): time.sleep(duration) else: await asyncio.sleep(duration) class SyncClient(BaseClient, zyncio.SyncMixin): pass class AsyncClient(BaseClient, zyncio.AsyncMixin): pass SyncClient.sleep(1.0) await AsyncClient.sleep(1.0)
Note
This decorator does not work with
ZyncDelegator.
- class zproperty[T_co, ReturnT_co](getter)¶
Wrap a method to act as a property in sync mode, and as a coroutine in async mode.
Decorated methods act like sync
propertys onSyncObjects and like async methods onAsyncObjects.Accessing on an instance of
AsyncObjectreturns aBoundZyncMethod.When accessed via the owning class, instances of
zpropertyare callable, calling through to the wrapped (unbound) getter.Example
class BaseClient: ... @zyncio.zproperty async def status(self) -> str: return await self.get_status.z() class SyncClient(BaseClient, zyncio.SyncMixin): pass class AsyncClient(BaseClient, zyncio.AsyncMixin): pass print(SyncClient().status) print(await AsyncClient().status())
- Parameters:
getter (
async(T_co)->ReturnT_co) – The getter for this property.
- setter(setter)¶
Return a new
ZyncSettablePropertywith the given setter.Example
class BaseClient: ... @zyncio.zproperty async def status(self) -> str: return await self.get_status.z() @status.setter async def status(self, value: str) -> None: await self.set_status.z(value) class SyncClient(BaseClient, zyncio.SyncMixin): pass class AsyncClient(BaseClient, zyncio.AsyncMixin): pass SyncClient().status = 'RUNNING' await AsyncClient().status.set('RUNNING')
Warning
Some type checkers may complain if you use the same name for the getter and setter.
- Return type:
- class ZyncSettableProperty[T, ReturnT](getter, setter)¶
A
zyncio.zpropertywith a setter.Accessing on an instance of
AsyncObjectreturns aBoundZyncSettableProperty.
- class zgeneratormethod[T_co, **P, ReturnT_co, SendT_contra](func)¶
Wrap a generator method to be callable in both sync and async mode.
Decorated methods return sync generators when called on
SyncObjects and async generators when called onAsyncObjects.Accessing on an instance returns a
BoundZyncGeneratorMethod.Example
class BaseClient: @zyncio.zgeneratormethod async def countdown(start: int) -> AsyncGenerator[int]: for i in range(start, 0, -1): if zyncio.is_sync(self): time.sleep(1.0) else: await asyncio.sleep(1.0) yield i class SyncClient(BaseClient, zyncio.SyncMixin): pass class AsyncClient(BaseClient, zyncio.AsyncMixin): pass for n in SyncClient().countdown(5): print(n) async for n in AsyncClient().countdown(5): print(n)
- Parameters:
func (
(T_co,*P.args,**P.kwargs)->AsyncGenerator[ReturnT_co,SendT_contra]) – The generator method to wrap.
- class zcontextmanagermethod[T_co, **P, ReturnT_co](func)¶
Similar to
@contextlib.contextmanager, but callable in both sync and async modes.Decorated methods return sync context managers when called on
SyncObjects and async context managers when called onAsyncObjects.Accessing on an instance returns a
BoundZyncContextManagerMethod.Example
class BaseClient: @zyncio.zcontextmanagermethod async def run_process(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() class SyncClient(BaseClient, zyncio.SyncMixin): pass class AsyncClient(BaseClient, zyncio.AsyncMixin): pass with SyncClient().run_process('some command') as pid: print(pid) async with AsyncClient().run_process('some command') as pid: print(pid)
- Parameters:
func (
(T_co,*P.args,**P.kwargs)->AsyncGenerator[ReturnT_co,None]) – The generator method to wrap.
Mixins and Protocols¶
- class SyncMixin¶
Mixin that makes bindable ZyncIO constructs use
SYNCmode.See the documentation for each construct for details on how they interact with this mixin.
- class AsyncMixin¶
Mixin that makes bindable ZyncIO constructs use
ASYNCmode.See the documentation for each construct for details on how they interact with this mixin.
- protocol ZyncDelegator¶
Protocol for delegating ZyncIO
Modeto another object.This protocol should be used for objects that wrap another ZyncIO-compatible object, and perform all operations via that “delegate” object.
This protocol is runtime checkable.
Classes that implement this protocol must have the following methods / attributes:
- abstractmethod __zync_delegate__()¶
Return an object to which calls to
zyncio.get_modeshould be delegated.The returned object must either be an instance of
zyncio.SyncMixinorzyncio.AsyncMixin, or implement this protocol itself.- Return type:
Bound Methods¶
- class BoundZyncMethod[T_co, **P, ReturnT_co](func, instance)¶
A bound
zyncio.zmethod.Acts like a sync method when bound to a
SyncObjectand like an async method when bound to anAsyncObject.- Parameters:
- await call_zync(*args, **kwargs)¶
Run the method as a coroutine regardless of mode.
- Parameters:
- Return type:
- class BoundZyncClassMethod[T, **P, ReturnT_co](func, cls)¶
A bound
zyncio.zclassmethod.Acts like a sync
classmethodwhen bound to a subclass ofSyncMixinand like an asyncclassmethodwhen bound to a subclass ofAsyncMixin.- Parameters:
- await call_zync(*args, **kwargs)¶
Run the method as a coroutine regardless of mode.
- Parameters:
- Return type:
- class BoundZyncSettableProperty[T, ReturnT](getter, setter, instance)¶
A bound
zyncio.ZyncSettableProperty.This class provides the
setfunctionality forZyncSettablePropertywhen accessed on an instance ofAsyncObject.- Parameters:
func – The method to wrap.
instance (
T) – The instance to bind the method to.
- await set(value)¶
Set the value of the property.
- class BoundZyncGeneratorMethod[T, **P, ReturnT_co, SendT_contra](func, instance)¶
A bound
zyncio.zgeneratormethod.Returns a sync generator when bound to a
SyncObjectand an async generator when bound to anAsyncObject.- Parameters:
func (
(T,*P.args,**P.kwargs)->AsyncGenerator[ReturnT_co,SendT_contra]) – The method to wrap.instance (
T) – The instance to bind the method to.
- call_zync(*args, **kwargs)¶
Run the generator function in the given mode.
- Return type:
- class BoundZyncContextManagerMethod[T, **P, ReturnT_co](func, instance)¶
A bound
zyncio.zcontextmanagermethod.Returns a sync context manager when bound to a
SyncObjectand an async context manager when bound to anAsyncObject.- Parameters:
func (
(T,*P.args,**P.kwargs)->AsyncGenerator[ReturnT_co,None]) – The method to wrap.instance (
T) – The instance to bind the method to.
- call_zync(*args, **kwargs)¶
Enter the context manager as an async context manager regardless of mode.
- Return type: