-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhello_world.py
More file actions
55 lines (37 loc) · 1.35 KB
/
hello_world.py
File metadata and controls
55 lines (37 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
"""Minimal vgi-rpc example: define a service and call it in-process.
This is the quickest way to get started. The service runs in a background
thread and communicates over an in-process pipe — no subprocess or network
needed.
Run::
python examples/hello_world.py
"""
from __future__ import annotations
from typing import Protocol
from vgi_rpc import serve_pipe
# 1. Define the service interface as a Protocol class.
# Return types determine the method type (unary here).
class Greeter(Protocol):
"""A simple greeting service."""
def greet(self, name: str) -> str:
"""Return a greeting for *name*."""
...
def add(self, a: float, b: float) -> float:
"""Add two numbers."""
...
# 2. Implement the interface.
class GreeterImpl:
"""Concrete implementation of the Greeter service."""
def greet(self, name: str) -> str:
"""Return a greeting for *name*."""
return f"Hello, {name}!"
def add(self, a: float, b: float) -> float:
"""Add two numbers."""
return a + b
# 3. Start the server in-process and call methods through a typed proxy.
def main() -> None:
"""Run the example."""
with serve_pipe(Greeter, GreeterImpl()) as svc:
print(svc.greet(name="World")) # Hello, World!
print(svc.add(a=2.5, b=3.5)) # 6.0
if __name__ == "__main__":
main()