Serve a Subject¶
from mesh_sdk import Mesh
async def add(data, msg):
x = json.loads(data)
return json.dumps({"sum": x["a"] + x["b"]}).encode()
async with Mesh("cluster-b", nats_url, bridge_url, token) as mesh:
await mesh.advertise("cluster-b.add", add, inter_cluster=True,
services=["compute.add"], metadata={"version": 1}, queue="workers")
await asyncio.Event().wait()
| Argument | Meaning |
|---|---|
subject |
Subject of the local cluster |
handler |
Function with data and message. Its result is the reply. None sends none |
inter_cluster |
Advertise to other clusters. Otherwise the subject is local |
services |
Up to 50 names of services |
metadata |
JSON object up to 16 KiB |
queue |
Queue group |
Scale¶
Start several processes with the same queue. Each message is handled by one of them, also for callers in other clusters.
Errors¶
If the handler raises an exception, the caller receives MeshError with the type of the exception. The text of the exception is not sent.
Withdraw¶
await mesh.withdraw("cluster-b.add")
The bridge refuses the subject at once and publishes a deletion to the relays.