Skip to main content

A Python RPC Framework

Project description

# Features
* Easy registration of methods
* Custom Exception and Options
* Includes plenty testing helpers

# Installation
On most systems, its a matter of
pip install qrpc

# Quickstart
## A Simple Example
This is a realy simple example of how to create your own API using QRpc

### Server

from qrpc.server import Server

server = Server()

def hello(name=None):
if name:
return "hello " + name
return "hello anonymous"

def add(x, y):
return x + y'', 8080)

### Client

from qrpc.client import RpcClient
from qrpc.client import ServerProxy

server = ServerProxy('http://localhost:8080/v1/batch')
rpc = RpcClient(server=server)
result ='ycc')

# Core content
## Request

## Response(Result)
The response of an rpc request has three attributes that user should concern, rpc_code, data, message.

The rpc_code indicates if the rpc request has been successfully received, understood, and accepted. 0 and all the positive numbers are **reserved code**, and can't be used by user.

The data is the result of an rpc method.

The message provides some helpful information.

## Exceptions
You can define an RPCException and raise it when you want to tell the caller there is something wrong in some rpc method. For example a division rpc method and the second argument of a division is zero.

# server
from qrpc.exceptions import RPCFaultException
def test_div(x, y):
if y == 0:
raise RPCFaultException(
code=99, # Use any code in your as you like except reserved code.
message="ZeroDivisionError: integer division or modulo by zero"
return x / y

# client
div_result =, y=0)

The QRpc will catch the exception and wrap it in reponse.

#Lazy Call and Evaluation

RPC call are lazy--the act of creating an rpc call doesn't send the network request to server. You can stack call together all day long, and the framework won't actually send the network request until one of the calls is evaluated. You can get detail from the following example:

add_result =, y=2)
dict_result ={"test_key": "test_value"})
hello_result ='world')
print ( # only one network request
print (
print (

Though this looks like sending three rpc call request, in fact it only send one network request, at the "" line. An rpc call is just added into a job list when it is constructed. The real network request will be executed when any of the 'rcp call' in the job list is evaluated. The framework evaluates all the rpc call in the job list at one time. So only send one network request.You can evaluate an rpc call by get any attribute of the rcp result.

In the last example, there are three rpc calls in the job list. The three rpc calls are evaluated at one network request when you get the data of add_result.
So or won't cause any network request.

In general, the result of an rpc call isn't fetched from the server until you ask them.

# Adcanced Usage

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

qrpc-0.1.0.tar.gz (8.7 kB view hashes)

Uploaded source

Built Distribution

qrpc-0.1.0-py2-none-any.whl (12.8 kB view hashes)

Uploaded py2

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page