Skip to content

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.

Create hello_service.py with the shared type and service definitions:

hello_service.py
from dataclasses import dataclass
from aster import service, rpc, wire_type
@wire_type("hello/HelloRequest")
@dataclass
class HelloRequest:
name: str = ""
@wire_type("hello/HelloResponse")
@dataclass
class HelloResponse:
message: str = ""
@service
class HelloService:
"""Simple greeting service."""
@rpc
async def say_hello(self, req: HelloRequest) -> HelloResponse:
return HelloResponse(message=f"Hello, {req.name}!")

Line by line:

  • @dataclass defines plain data types for the request and response. Fields have defaults so Fory can construct them during deserialization.
  • @service marks the class as an Aster RPC service. The service name defaults to the class name (HelloService), version defaults to 1.
  • @rpc marks say_hello as a unary RPC method. The framework inspects the type annotations (HelloRequest and HelloResponse) 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.

Create producer.py to host the service:

producer.py
import asyncio
from hello_service import HelloService
from 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 with starts the server; awaiting serve() keeps this standalone producer running. Leaving the context closes the server.
  • srv.address is the aster1... connection address that consumers need to connect.

Create consumer.py to call the service:

consumer.py
import asyncio
from hello_service import HelloService, HelloRequest
from 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 from ASTER_ENDPOINT_ADDR, set below. The address identifies the peer and provides connection hints.
  • async with calls connect(), which creates a QUIC endpoint and runs the admission handshake to discover available services.
  • client.client(HelloService) returns a typed stub. The stub’s say_hello method serializes the request, sends it over QUIC, deserializes the response, and returns 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:

Terminal window
# Install the preview wheel for your platform first; see the quickstart.
python producer.py

Output:

Endpoint address: <actual address printed by the producer>
Waiting for connections... (Ctrl+C to stop)

Terminal 2 – consumer:

Terminal window
export ASTER_ENDPOINT_ADDR="<paste the complete producer address>"
python consumer.py

Output:

Hello, World!
  1. The producer starts a node and publishes HelloService. Its printed address contains the endpoint identity and connection hints.
  2. 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.
  3. client.client(HelloService) supplies a typed interface. Calling say_hello serializes HelloRequest, sends the RPC, and waits for a response.
  4. The server dispatches the request to your Python method and returns its HelloResponse. The consumer prints Hello, World! and closes its node.
  5. 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.