yr.InstanceProxy.snapshot

yr.InstanceProxy.snapshot#

InstanceProxy.snapshot(ttl: int = -1, leave_running: bool = False) str[source]#

Create instance snapshot.

This method triggers a snapshot of the current instance state, sending signal 18 (INSTANCE_SNAPSHOT_SIGNAL) through the Kill interface. The snapshot can be used later to restore the instance to this exact state.

Parameters:
  • ttl (int, optional) – Time-to-live for the snapshot in seconds. Default is 600 seconds.

  • leave_running (bool, optional) – Whether to keep the instance running after snapshot. - If True: Instance continues running after snapshot (online snapshot) - If False: Instance will be terminated after snapshot (offline snapshot) Default to False.

Returns:

The checkpoint ID that uniquely identifies this snapshot.

Format: {instanceID}-{functionID}-{uuid}

Return type:

str

Raises:

RuntimeError – If the instance is not active or snapshot operation fails.

Example

>>> import yr
>>> yr.init()
>>>
>>> @yr.instance
... class MyInstance:
...     def __init__(self):
...         self.counter = 0
...
...     def increment(self):
...         self.counter += 1
...
...     def __yr_before_snapshot__(self):
...         print(f"Preparing snapshot, counter={self.counter}")
...
>>> ins = MyInstance.invoke()
>>> yr.get(ins.increment.invoke())
>>> checkpoint_id = ins.snapshot(leave_running=False)
>>> print(f"Snapshot created: {checkpoint_id}")
>>>
>>> yr.finalize()