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 using msgpack.
# Define Pydantic models
class FileData(BaseModel):
filename: str
data: bytes
Define a class using the @endpoint decorator to specify which methods will be
accessible via rpc.
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
def get_file_names(self) -> List[str]:
return list(self.files.keys())
@endpoint
async def download_file(self, name: str) -> FileData:
return self.files[name]
Use the create_server function to make an instance of your server-side rpc
class. The server instance will be assigned an rpc attribute to access server
functions like connect and disconnect. Once connect is called methods
decorated with @endpoint will be invoked automatically by remote calls from
the client.
NOTE: The
RpcProvider.connectmethod is non-blocking.
server = create_server(Dropbox, max_files=2)
# Returns once connected to redis
await server.rpc.connect(dsn="redis://localhost")
# Wait forever
while True:
await asyncio.sleep(1)
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.
NOTE: If there are no subscribers to the redis channel then the client will throw a
RpcNotConnectedError.
client = create_client(Dropbox)
await client.rpc.connect(dsn="redis://localhost")
Now that both ends are connected 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)
Metadata
Release files for aioredis-rpc 1.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| aioredis-rpc-1.1.0.tar.gz | 9.3 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| aioredis_rpc-1.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 18.4 kB
Release files / aioredis-rpc-1.1.0.tar.gz
| Download URL | aioredis-rpc-1.1.0.tar.gz |
|---|---|
| Size | 9.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
993f3344d062e51098bbaa405e626edddb3216eae4f23f69f635dd86afa01b72
|
|
BLAKE2b-256 checksum How to use checksums |
39b21ca4233c318909aa4d1ab4f559015fa7223f82a4c3d5ae08e4e2567b1087
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.1 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.8.11
|
Release files / aioredis_rpc-1.1.0-py3-none-any.whl
| Download URL | aioredis_rpc-1.1.0-py3-none-any.whl |
|---|---|
| Size | 9.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
c23cd5193988fa6a2442f6bfd43d9b0a2acb07bbf986f9bbafc5726eecfeb483
|
|
BLAKE2b-256 checksum How to use checksums |
f887c2aa95e43cce19695f223f6afe33b8279ebd191aa921a4439eb6311a7ee2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.1 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.8.11
|