An RPC library based on aioredis, msgpack, and pydantic.
Project description
aioredis-rpc
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)
Development
The following items will be handled in future revisions.
- Support generic return-types so
list
and other container types may be returned from endpoints.
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
Built Distribution
Hashes for aioredis_rpc-1.0.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3f91f30656ca55e97fb04039c010df80d38762ff2a346c41532c5cf487db615c |
|
MD5 | c0e34652c6b6ba6558d05a8ce92ed729 |
|
BLAKE2b-256 | 1997ef6ffae71ec1315a7ead62dd19e2420dca50bb11bc08c7401e323f2696f5 |