This library is an asynchronous python library for the JSON-RPC specification.
It is licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0.html).
Install from PyPi via pip:
pip3 install async4jsonrpc
- Compliant with the JSON-RPC 2.0 specification
- High performance by asyncio and coroutine
- Json serialization support via jsonpickle
from async4jsonrpc.server import JsonRpcServer
# create RPC json server
server = JsonRpcServer()
# registers a function to respond to RPC requests.
server.register_function(lambda x, y: x + y, 'add')
server.register_function(lambda x: x, 'ping')
# start to serve RPC request
server.serve('localhost', 8080)
import asyncio
from async4jsonrpc.client import JsonRpcClient
# create RPC json client
client = JsonRpcClient('127.0.0.1', 8080)
async def call_rpc(i):
# rpc: send rpc request
r = await client.ping(f'hello {i}')
print(r)
asyncio.run(call_rpc('shi'))
I use jsonpickle library for serialization and deserialization of complex Python objects to and from JSON
- Python object class example: thing.py
class Thing(object):
def __init__(self, name):
self.name = name
- Client call rpc with a python object: client-test.py
import asyncio
from async4jsonrpc.client import JsonRpcClient
from tests.thing import Thing
# create RPC json client
client = JsonRpcClient('127.0.0.1', 8080)
async def call_object_rpc():
obj = Thing('Awesome')
r = await client.ping(obj)
print(r)
asyncio.run(call_object_rpc())
- improve