Skip to content

Instantly share code, notes, and snippets.

@hcarty
Created November 29, 2025 20:30
Show Gist options
  • Select an option

  • Save hcarty/7a2b6471e76642b625c324a26d7b3446 to your computer and use it in GitHub Desktop.

Select an option

Save hcarty/7a2b6471e76642b625c324a26d7b3446 to your computer and use it in GitHub Desktop.
pocketpy RPC experiment - incomplete
import pkpy
class Rpc[A, R]:
def __init__(self, vm: pkpy.ComputeThread, function: str) -> None:
self._vm = vm
self._function = function
def call(self, arg: A) -> None:
if not self._vm.is_done:
raise ValueError("vm is busy")
self._vm.submit_call(self._function, arg) # pyright: ignore[reportUnknownMemberType]
def result(self) -> R | None:
if not self._vm.is_done:
return None
return self._vm.last_retval() # pyright: ignore[reportUnknownMemberType, reportUnknownVariableType]
def machine(self, args: Generator[A]) -> Generator[R | None]:
while not self._vm.is_done:
yield None
self.call(next(args))
while True:
result = self.result()
if result is not None:
self.call(next(args))
yield result
vm = pkpy.ComputeThread(1)
vm_src = """
def double(x: float) -> float:
return x * 2.0
"""
vm.exec(vm_src)
rpc_double = Rpc[float, float](vm, "double")
def arguments() -> Generator[float]:
x = 0.0
while True:
yield x
x += 1.0
double_machine = rpc_double.machine(arguments())
while True:
remote = next(double_machine)
if remote is not None:
print(f"Calculated {remote}")
else:
print("Waiting")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment