Skip to main content

An RPC library based on aioredis, msgpack, and pydantic.

Project description

aioredis-rpc

ci coverage pypi downloads versions license

A RPC interface using aioredis and pydantic.

Usage

pip install aioredis-rpc

pydantic is used to model complex objects which are transparently serialized and packed into messages.

# Define Pydantic models
class FileData(BaseModel):
  filename: str
  data: bytes

Define a class using the @endpoint decorator to specify which methods will be accessible over the rpc interface.

from redisrpc import endpoint

# Define an RPC class
class Dropbox:
  files: Dict[str, FileData]
  max_files: int

  def __init__(self, max_files: int = 1000):
    self.files = dict()
    self.max_files = max_files

  @endpoint
  async def upload_file(self, file: FileData) -> int:
    if len(self.files) >= self.max_files:
      # Errors are propagated to the client-side
      raise Exception('too many files')
    self.files[file.name] = file
    return len(file.data)

  @endpoint
  async def download_file(self, name: str) -> FileData:
    return self.files[name]

Use create_server function to create an instance of your server-side rpc class. The server instance will be assigned an rpc: RpcProvider attribute to access server functions like connect and disconnect. Once connect is called methods decorated with @endpoint will be invoked automatically by client RPC messages.

Also note that connect is non-blocking.

server = create_server(Dropbox, max_files=2)
# Returns once connected to redis
await server.rpc.connect(dsn="redis://localhost")

The create_client function create a faux instance of the rpc class with only the methods decorated by @endpoint present. When these methods are called by the client the function arguments are serialized and published to redis.

client = create_client(Dropbox)
await client.rpc.connect(dsn="redis://localhost")

Now that both ends are connected the the @endpoint decorated methods may be called like they are accessing the actual class passed to create_client.

file1 = FileData(name='file1', data=b'1234')
size = await client.upload_file(file1)

Download files

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

Source Distribution

aioredis-rpc-0.2.0.tar.gz (7.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

aioredis_rpc-0.2.0-py3-none-any.whl (8.0 kB view details)

Uploaded Python 3

File details

Details for the file aioredis-rpc-0.2.0.tar.gz.

File metadata

  • Download URL: aioredis-rpc-0.2.0.tar.gz
  • Upload date:
  • Size: 7.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.8.10

File hashes

Hashes for aioredis-rpc-0.2.0.tar.gz
Algorithm Hash digest
SHA256 f30e6d1d7b8a07c44fbc88875c7f38b92317919e26e09103ef8ee73705f972e3
MD5 41d3145e87d62067bf8e5bb4b7a18e9f
BLAKE2b-256 bb6888a64c904cc169524b98506e4937fbc33ac8a8ceb4f745809175003e0426

See more details on using hashes here.

File details

Details for the file aioredis_rpc-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: aioredis_rpc-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 8.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.8.10

File hashes

Hashes for aioredis_rpc-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 59e1af0b3553c4d64f00d82fe86781a7803270b4082f7c2023a45d819d27f20d
MD5 7ff04ad922dff04e5e57c7fa5e659b45
BLAKE2b-256 d0029515177d035e1341d797017cb4624e92f99ab3ddef50afba61195a4845c0

See more details on using hashes here.

Supported by

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