CarrotRPC it is a python asyncio RPC server/client for RabbitMQ with pydantic schema that allows you to make RPC calls.
Project description
Carrot
Carrot-RPC it is a python asyncio RPC server/client for RabbitMQ that allows you to make RPC calls.
Note: This project in active development and can be unstable.
You can see live examples here:
http://5.187.4.179:5888/ (https://github.com/Sobolev5/Carrot-RPC-Example)
http://89.108.77.63:1025/ (https://github.com/Sobolev5/LordaeronChat)
https://github.com/Sobolev5/carrot-rpc
Install
To install run:
pip install carrot-rpc
Microservice A which call
import asyncio
from carrot import CarrotCall
from simple_print import sprint
# set amqp connection:
AMQP_URI = "amqp://admin:password@127.0.0.1/vhost"
# defer function which call «MICROSERVICE_SUM»:
async def call_sum_a_and_b():
# make dict request:
dct = {}
dct["caller"] = "Function on microservice_interface which call RPC in microservice_sum"
dct["number_a"] = int(data["number_a"])
dct["number_b"] = int(data["number_b"])
# defer carrot instance and make rpc call:
carrot = await CarrotCall(AMQP_URI).connect()
response_from_another_microservice = await carrot.call(dct, "microservice_sum:sum_a_and_b", timeout=5)
# first arg is dict with data
# second arg it routing key (through default AMQP exchange)
# third arg is optional (response timeout in seconds, 5 seconds by default)
# get response dict from microservice «MICROSERVICE_SUM»
sprint(f'Sum a and b: {response_from_another_microservice["sum"]}', c="yellow")
# you can send request to another microservice without reply (like standart call):
await carrot.call(dct, "microservice_sum:sum_a_and_b", without_reply=True)
# in this case «MICROSERVICE_SUM» just calculate sum and do not send response to caller.
loop = asyncio.get_event_loop()
loop.run_until_complete(call_sum_a_and_b())
Microservice B which ask
import asyncio
import aiormq
from pydantic import BaseModel
from carrot import carrot_ask
from simple_print import sprint
from fastapi import FastAPI
from fastapi import APIRouter
# set amqp connection:
AMQP_URI = "amqp://admin:password@127.0.0.1/vhost"
# make pydantic schema:
class SumAAndB(BaseModel):
caller: str
number_a: int
number_b: int
# decorate called function with pydantic schema
@carrot_ask(SumAAndB)
async def sum_a_and_b(incoming_dict: dict) -> dict:
sprint(incoming_dict, c="green")
dct = {}
dct["caller"] = "i am sum_a_and_b function mounted on microservice_sum"
dct["sum"] = incoming_dict["number_a"] + incoming_dict["number_b"]
return dct
# make amqp router:
async def amqp_router():
connection = await aiormq.connect(AMQP_URI)
channel = await connection.channel()
sprint(f"AMQP: ready [yes]", c="green")
sum_a_and_b__declared = await channel.queue_declare(f"microservice_sum:sum_a_and_b", durable=False)
await channel.basic_consume(sum_a_and_b__declared.queue, sum_a_and_b, no_ack=False)
class App(FastAPI):
def __init__(self, *args, **kwargs):
loop = asyncio.get_event_loop()
loop.create_task(amqp_router())
super().__init__(*args, **kwargs)
app = App()
Full working example [1]
https://github.com/Sobolev5/Carrot-RPC-Example
Full working example [2]
https://github.com/Sobolev5/LordaeronChat
TODO
tests, docstrings, extended documentation
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
File details
Details for the file carrot_rpc-0.3.1-py3-none-any.whl
.
File metadata
- Download URL: carrot_rpc-0.3.1-py3-none-any.whl
- Upload date:
- Size: 6.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.10.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | abb5b41befb7f7ced1d0533de87e0b2d4fc45a778962ea609053e853e0268f92 |
|
MD5 | f8371a287cf068d07f7813c2c84e2e5c |
|
BLAKE2b-256 | c2e23d4d614bec32c3da5c0454a5e8aebb081a8fca1922a97ded0b695383c146 |