Skip to main content

RPC over redis communication library

Project description

jarpc

Warning: project is in early development stage, expect bugs and frequent breaking changes

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

Documentation Status Build Status codecov

Features

  • Client, Server and Slient connection modes.
Client Server Slient
Calling commands yes no yes
Receiving commands no yes yes
  • asyncronous response processing (AsyncIterator).
  • encoding customization (marshal (default), json, msgpack, pickle, ...).

Installation

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

Examples

Client

import asyncio

from jarpc import Client

REDIS_HOST = "localhost"
REDIS_PORT = 6379

COMMAND_PING = 0
COMMAND_SLOW_PING = 1
COMMAND_FIX_BUGS = 42


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()

    ping_data = {"message": input("Enter message to send or leave blank: ")}

    print("PING      ->", await client.call(COMMAND_PING, ping_data))
    print("SLOW_PING ->", await client.call(COMMAND_SLOW_PING, timeout=1))
    print("FIX_BUGS  ->", await client.call(COMMAND_FIX_BUGS))

    # exit
    client.close()


if __name__ == "__main__":
    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'."""

    print("Received PING command")

    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..."""

    print("Received SLOW_PING command")

    await asyncio.sleep(2)

    return "pong"


if __name__ == "__main__":
    server.run((REDIS_HOST, REDIS_PORT))

Slient

import os
import asyncio

from jarpc import Slient, Request

REDIS_HOST = "localhost"
REDIS_PORT = 6379

COMMAND_PING = 0

loop = asyncio.get_event_loop()

slient = Slient(
    "example",
    loop=loop,
    node=f"example-{os.getpid()}",
    default_timeout=5,
    default_expect_responses=1,
)


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

    print("Received PING command")

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


async def call_ping(slient: Slient) -> None:
    await slient.wait_until_ready()

    print("PING ->", await slient.call(COMMAND_PING))


if __name__ == "__main__":
    loop.create_task(slient.start((REDIS_HOST, REDIS_PORT)))
    loop.create_task(call_ping(slient))

    # continue listening for commands
    loop.run_forever()

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

jarpc-0.3.1.tar.gz (9.6 kB view details)

Uploaded Source

Built Distribution

jarpc-0.3.1-py3-none-any.whl (29.8 kB view details)

Uploaded Python 3

File details

Details for the file jarpc-0.3.1.tar.gz.

File metadata

  • Download URL: jarpc-0.3.1.tar.gz
  • Upload date:
  • Size: 9.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.37.0 CPython/3.6.7

File hashes

Hashes for jarpc-0.3.1.tar.gz
Algorithm Hash digest
SHA256 714c02e8ae1f99a1c7943a016b700529e6c78004cf34c2aa67bdcc41cf29bcad
MD5 358d1da051b7c137934eb5199882b4cf
BLAKE2b-256 2450a9e2c2f1320a78b1c5c9d2f419303d6c7cf3dd4ee93538252d1a1797acf5

See more details on using hashes here.

File details

Details for the file jarpc-0.3.1-py3-none-any.whl.

File metadata

  • Download URL: jarpc-0.3.1-py3-none-any.whl
  • Upload date:
  • Size: 29.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.37.0 CPython/3.6.7

File hashes

Hashes for jarpc-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 29b48f104edf7cd28d72e348a52a56ec41acb96f0ad1771efca6367c18b62665
MD5 20fe3a8b2e26da9721c97cfff6699dbd
BLAKE2b-256 101210197ea7e58b1f3eb1ffc9d23e0d6e81b398a61c5a68826265c9f64aac25

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page