based on https://github.com/joshmarshall/tornadorpc
This is a experimental project, which tends to change frequently. Caveat Emptor!
register(func, name:str=None, timeout:(int, float)=None)Try to use python3 server.py -h to see what optional arguments you can offer.
# Server settings
rpc_port: 10080
# RPC settings
company: github
service: userA simple example:
def add_user(username: str, password: str):
if username == 'test':
return True
else:
raise Exception('test exception: Username invalid')Type hints are optional, if you specify them, parameter types will be checked in runtime. Asyncio-JSONRPC can detect your handler's type, if handler
- Type is
asyncio.coroutine,await handler(*args, **kwargs)will be invoked - Type isn't
asyncio.coroutine- if handler has attribute
_new_process, it will be executed in a ProcessExecutorPool - else handler will be executed in a ThreadExecutorPool.
- if handler has attribute
Add attribute _need_authenticated with value is auth_handler, auth_handler is a function which has two keyword
parameters username, password, it should return True or False to check if user should be granted.
Create a file (server.py or something else)
from asynciorpc.interface.register import register
from asynciorpc.runner import run
# put your implement codes to implements package
# import here and register as an asynciorpc
class TestClass:
def test(self, a, b):
import time
time.sleep(10)
return (1, a, b)
def test2(self, a, b):
return (1, a, b)
a = TestClass()
register(a.test, 'TestApi') # http://127.0.0.1:10080/github.user.TestApi
register(a.test2, 'TestApi2') # http://127.0.0.1:10080/github.user.TestApi2
run()Example
In your server.py
from asynciorpc.runner import run
REGISTER_YOUR_API
app = run(wsgi=True)And run gunicorn
gunicorn server:app --bind localhost:8080 --worker-class aiohttp.worker.GunicornWebWorkerRef: http://aiohttp.readthedocs.org/en/stable/gunicorn.html
handler must return python builtin types, such as int, float, str, dict, list, others like datetime.datetime
are not supported, JSON serialization exceptions will be raised.