Implementation of RPC over MQTT.
Run the broker:
cd ./broker && ./run.shCreate a new app instance (app.py):
from mqtt_rpc import mqtt_rpc
app = mqtt_rpc.MqttRpc("mytest", ["tasks"], {"hostname": "localhost"})Run the worker:
python -m mqtt_rpc --app app.app workerWrite a task (tasks.py):
from app import app
@app.task
def sum(a, b) -> int:
return a + bSend a task to the worker:
import asyncio
import tasks
async def main() -> None:
res = await tasks.sum.apply_sync(args=[1, 3])
print(await res.get())
asyncio.run(main())Result:
Response(id='63408401-f95d-437b-9810-4e5a2b3335ae', result=4, status=<Status.SUCCESS: 2>)