Library to work with RPC
Project description
kode_rpc
Server Usage
from typing import Mapping
import asyncio
import logging
from kode_rpc.method import RPCMethod, method
from kode_rpc.server import RPCServer, RPCRequestInfo
@method(version=1)
async def some_rpc_method(param: str):
return {'result': True}
async def rpc_logger(rpc_method: RPCMethod, result: Mapping, request_info: RPCRequestInfo):
logging.info('rpc_log', extra={
'rpc_name': rpc_method.name,
'rpc_result': result,
'trace_id': request_info.trace_id,
'master': request_info.master
})
async def rpc_exception_handler(rpc_method: RPCMethod, exc: BaseException, request_info: RPCRequestInfo) -> Mapping:
if isinstance(exc, RuntimeError):
return {
'error': {
'code': 'RuntimeError',
'trace_id': request_info.trace_id
}
}
else:
return {
'error': {
'code': 'ServerError',
'trace_id': request_info.trace_id
}
}
async def main():
logging.basicConfig(level=logging.INFO)
server = RPCServer('my_service', rabbitmq_host='localhost', rabbitmq_user='guest', rabbitmq_password='guest')
server.method_logger(rpc_logger)
server.method_exception_handler(rpc_exception_handler)
await server.register_methods([some_rpc_method])
await server.serve()
if __name__ == '__main__':
asyncio.run(main())
Client Usage
import asyncio
from kode_rpc.client import RPCClient
# To access rpc_client from anywhere it must be initialized at top level module
rpc_client = RPCClient('client_name')
async def main():
# Connect to rpc broker once at startup
await rpc_client.connect(rabbitmq_host='localhost', rabbitmq_user='guest', rabbitmq_password='guest')
# ...
# Somewhere in logic
result = await rpc_client.call(
service_name='my_service',
method='rpc_method',
payload={'param': 'any'}
)
# => {'result': True}
# When application is shutting down do not forget to disconnect from rpc broker
await rpc_client.disconnect()
if __name__ == '__main__':
asyncio.run(main())
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
kode_rpc-0.0.8.tar.gz
(5.1 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file kode_rpc-0.0.8.tar.gz.
File metadata
- Download URL: kode_rpc-0.0.8.tar.gz
- Upload date:
- Size: 5.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.8.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
496f4de686f412182752c1a1bcde1a184313f4793482a6b5597af4c85e34e1b2
|
|
| MD5 |
a4b78fcd4228ae12d18d4642f9daea7b
|
|
| BLAKE2b-256 |
3c029a04a29dcd15a6a8d8092d2a47a9030b67ef1abac75d6ce5aed73989d612
|
File details
Details for the file kode_rpc-0.0.8-py3-none-any.whl.
File metadata
- Download URL: kode_rpc-0.0.8-py3-none-any.whl
- Upload date:
- Size: 6.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.8.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fc2146e3249ffa215583c34137ac02b02245158632781361d0b72232d809d325
|
|
| MD5 |
3fc26da8076d9ef78a494795a93de88d
|
|
| BLAKE2b-256 |
857ae952227df525a3fb7afb3679877f4c6685d18b7b01309706b482507822f5
|