IOMirea / jarpc

Just another RPC library
https://jarpc.readthedocs.io
GNU General Public License v3.0
5 stars 2 forks source link
python redis rpc

jarpc

Warning: project is in alpha, expect bugs and breaking changes until 1.0.0

jarpc - Just another python RPC library based on redis pubsub. It is built with aioredis.

Documentation Status Build Status codecov

Features

Client Server Slient
Calling commands yes no yes
Receiving commands no yes yes

Installation

Library can be installed from PyPi: pip install jarpc
Or from repository: pip install git+https://github.com/IOMirea/jarpc.git#egg=jarpc

Simple examples

Client

import asyncio

from jarpc import Client

REDIS_HOST = "localhost"
REDIS_PORT = 6379

COMMAND_PING = 0
COMMAND_SLOW_PING = 1

async def main() -> None:
    client = Client("example", default_timeout=5, default_expect_responses=1)

    asyncio.create_task(client.start((REDIS_HOST, REDIS_PORT)))

    await client.wait_until_ready()

    print("PING      ->", await client.call(COMMAND_PING, {"message": "hello"}))
    print("SLOW_PING ->", await client.call(COMMAND_SLOW_PING, timeout=1))

    # exit
    client.close()

asyncio.run(main())

Server

import os
import asyncio

from jarpc import Server, Request

REDIS_HOST = "localhost"
REDIS_PORT = 6379

COMMAND_PING = 0
COMMAND_SLOW_PING = 1

server = Server("example", node=f"example-{os.getpid()}")

@server.command(COMMAND_PING)
async def ping(req: Request, message: str = "") -> str:
    """Responds with provided message argument or 'pong'."""

    return "pong" if message == "" else message

@server.command(COMMAND_SLOW_PING)
async def slow_ping(req: Request) -> str:
    """Responds with 'pong' after 2 seconds, too slow..."""

    await asyncio.sleep(2)

    return "pong"

server.run((REDIS_HOST, REDIS_PORT))

More examples can be found in examples folder.

Dependencies

Documentation

Documentation is available at https://jarpc.readthedocs.io

Source code

Source code is available on GitHub: https://github.com/IOMirea/jarpc

Protocol specification

Soon

Contributing

Feel free to open an issue or submit a pull request.

License

Source code is available under GPL v3.0 license, you can see it here.