Skip to main content

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

Metadata

Release files for carrot-rpc 0.3.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distribution (wheel)

Table of built distributions (wheels) for carrot-rpc 0.3.1
File Interpreter ABI Platform Actions
carrot_rpc-0.3.1-py3-none-any.whl Python 3 none any Details for carrot_rpc-0.3.1-py3-none-any.whl

Release files / carrot_rpc-0.3.1-py3-none-any.whl

Download URL carrot_rpc-0.3.1-py3-none-any.whl
Size 6.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
abb5b41befb7f7ced1d0533de87e0b2d4fc45a778962ea609053e853e0268f92
BLAKE2b-256 checksum
How to use checksums
c2e23d4d614bec32c3da5c0454a5e8aebb081a8fca1922a97ded0b695383c146
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.2

Security for carrot-rpc 0.3.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page