StreamCall / image transfer / EventBus / event subscriptions
Development guide / Detailed gRPC guide / Home
Expose a communication-independent Python DeviceClass via gRPC dynamic dispatch. The example uses the create_grpc_server and GrpcClient interfaces present in the supplied framework source.
class Counter:
def __init__(self):
self.value = 0
def increment(self, amount: int = 1) -> int:
self.value += amount
return self.value
Pass an instance through ctrl_obj. Place this after the Counter definition.
from grpc_frame.grpc_server import create_grpc_server
ctrl = Counter()
server = create_grpc_server(ctrl_obj=ctrl, max_workers=10)
if server.add_insecure_port("127.0.0.1:50051") == 0:
raise RuntimeError("Cannot bind gRPC port")
server.start()
try:
server.wait_for_termination()
finally:
server.stop(grace=None)
from grpc_frame.grpc_client import GrpcClient
client = GrpcClient(server_ip="127.0.0.1", server_port=50051)
try:
print(client.increment(2))
finally:
client.close()
The client uses Describe to discover public methods and Call to invoke them. increment belongs to the DeviceClass, not the transport framework.
See the detailed guide for streaming, events, async clients, type stubs, and connection management. See the existing installation guide for packaging and installation.