Hello service
This example separates a Python application into three files: a shared service definition, a producer that hosts it, and a typed consumer that calls it. Use this layout when you want client-side autocomplete and explicit request types while keeping the producer and consumer in separate processes.
First complete the Python installation in the quickstart. Use one project directory and the same virtual environment in both terminals. This example deliberately permits unenrolled consumers; use test data and configure the trust model before deployment.
The service definition
Section titled “The service definition”Create hello_service.py with the shared type and service definitions:
from dataclasses import dataclassfrom aster import service, rpc, wire_type
@wire_type("hello/HelloRequest")@dataclassclass HelloRequest: name: str = ""
@wire_type("hello/HelloResponse")@dataclassclass HelloResponse: message: str = ""
@serviceclass HelloService: """Simple greeting service."""
@rpc async def say_hello(self, req: HelloRequest) -> HelloResponse: return HelloResponse(message=f"Hello, {req.name}!")Line by line:
@dataclassdefines plain data types for the request and response. Fields have defaults so Fory can construct them during deserialization.@servicemarks the class as an Aster RPC service. The service name defaults to the class name (HelloService), version defaults to1.@rpcmarkssay_helloas a unary RPC method. The framework inspects the type annotations (HelloRequestandHelloResponse) to handle serialization automatically.
The explicit wire names keep payload identity independent of the local Python module path. Both processes import the same definitions, so they agree on service and method names as well as payload fields.
The producer
Section titled “The producer”Create producer.py to host the service:
import asynciofrom hello_service import HelloServicefrom aster import AsterServer
async def main(): async with AsterServer(services=[HelloService()], allow_all_consumers=True) as srv: print(f"Endpoint address: {srv.address}") print("Waiting for connections... (Ctrl+C to stop)") await srv.serve()
asyncio.run(main())Line by line:
AsterServer(services=[HelloService()], allow_all_consumers=True)creates a producer that hosts one service. The server creates a node, registers the service, and explicitly permits unenrolled consumers for this example.async withstarts the server; awaitingserve()keeps this standalone producer running. Leaving the context closes the server.srv.addressis theaster1...connection address that consumers need to connect.
The consumer
Section titled “The consumer”Create consumer.py to call the service:
import asynciofrom hello_service import HelloService, HelloRequestfrom aster import AsterClient
async def main(): async with AsterClient() as client: hello = await client.client(HelloService) resp = await hello.say_hello(HelloRequest(name="World")) print(resp.message)
asyncio.run(main())Line by line:
AsterClient()reads the producer address fromASTER_ENDPOINT_ADDR, set below. The address identifies the peer and provides connection hints.async withcallsconnect(), which creates a QUIC endpoint and runs the admission handshake to discover available services.client.client(HelloService)returns a typed stub. The stub’ssay_hellomethod serializes the request, sends it over QUIC, deserializes the response, and returns it.
Running it
Section titled “Running it”Open two terminals in the project directory and activate the same Python environment. This producer explicitly accepts consumers without enrollment credentials.
Terminal 1 – producer:
# Install the preview wheel for your platform first; see the quickstart.python producer.pyOutput:
Endpoint address: <actual address printed by the producer>Waiting for connections... (Ctrl+C to stop)Terminal 2 – consumer:
export ASTER_ENDPOINT_ADDR="<paste the complete producer address>"python consumer.pyOutput:
Hello, World!Follow one call
Section titled “Follow one call”- The producer starts a node and publishes
HelloService. Its printed address contains the endpoint identity and connection hints. - The consumer connects using that address. The Python wrapper performs consumer admission and obtains service information. The explicit open flag permits this example’s unenrolled caller.
client.client(HelloService)supplies a typed interface. Callingsay_helloserializesHelloRequest, sends the RPC, and waits for a response.- The server dispatches the request to your Python method and returns its
HelloResponse. The consumer printsHello, World!and closes its node. - The producer continues serving. Stop it with Ctrl+C after the test.
A new producer identity can produce a new address after restart. Copy the
current address if a previously working consumer can no longer connect. If
Python cannot import aster, check which virtual environment is active. If
the service or method is missing, confirm both terminals use the same files.
Next steps
Section titled “Next steps”- Choose stable wire names and extend the contract: Define a Service
- Connect with credentials: Dial a Service
- Add streaming methods: Define a Service – Streaming