-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubprocess_worker.py
More file actions
60 lines (39 loc) · 1.34 KB
/
subprocess_worker.py
File metadata and controls
60 lines (39 loc) · 1.34 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
56
57
58
59
60
"""Subprocess server entry point.
This script serves an RPC service over stdin/stdout, designed to be spawned
as a child process by a client using ``vgi_rpc.connect()``.
The client example is in ``subprocess_client.py``.
Run the client (which spawns this automatically)::
python examples/subprocess_client.py
"""
from __future__ import annotations
from typing import Protocol
from vgi_rpc import run_server
class Calculator(Protocol):
"""Simple calculator service."""
def add(self, a: float, b: float) -> float:
"""Add two numbers."""
...
def multiply(self, a: float, b: float) -> float:
"""Multiply two numbers."""
...
def divide(self, a: float, b: float) -> float:
"""Divide a by b."""
...
class CalculatorImpl:
"""Concrete implementation of Calculator."""
def add(self, a: float, b: float) -> float:
"""Add two numbers."""
return a + b
def multiply(self, a: float, b: float) -> float:
"""Multiply two numbers."""
return a * b
def divide(self, a: float, b: float) -> float:
"""Divide a by b."""
if b == 0.0:
raise ValueError("Division by zero")
return a / b
def main() -> None:
"""Serve over stdin/stdout."""
run_server(Calculator, CalculatorImpl())
if __name__ == "__main__":
main()