mon4ter / sanic-jsonrpc Goto Github PK
View Code? Open in Web Editor NEWJSON-RPC 2.0 support for Sanic over HTTP and WebSocket
License: MIT License
JSON-RPC 2.0 support for Sanic over HTTP and WebSocket
License: MIT License
eg:
`
@jsonrpc.jrpc
async def sub(a: int, b: int) -> List[Dict[str, int]]:
await asyncio.sleep(0.1)
return [{"a": a, "b": b}]
`
Sub (5,2) is called correctly at this time
2.`
@jsonrpc.jrpc
async def sub(a: int, b: int) -> List[Dict[str, int]]:
await asyncio.sleep(0.1)
return [{"a": a, "b": b, "c": "c"}]
`
Call error at this time.
feshttp.jsonrpc._routing.route.ResultError: invalid call result value [{'a': 5, 'b': 2, 'c': 'c'}]
In my usage scenario, a lot of data is directly read from the database. There is no need to verify the type of return value. I just need to know that it is a Dict
Once verified, some performance will be lost, so I suggest that you can choose to turn on the verification function
Is there an example for rpc streaming?
Thank you
Hi, I made a subclass of SanicJsonrpc to help cancel long-runing-tasks. https://github.com/hyansuper/cancellable-sanic-jsonrpc
the idea is the client issues an request with an unique task id in 'params' field, (not the message id of jsonrpc spec), server then register this id as its task name, then the client can later issue another jsonrpc calling a remote cancel function with the same task id to cancel the previous long-running-task on server, if it's still running. see client_test.py
Basically, it works, but I need some help in my code:
note: server_test.py requires python3.8 because the asyncio.current_task().set_name()
is only added in version3.8
Can you help? Thank you
ps, please ignore my two previous issues.
hello,mon4ter
`
import asyncio
from sanic import Sanic
from sanic_jsonrpc import Jsonrpc
app = Sanic()
jsonrpc = Jsonrpc(app)
@jsonrpc
async def sub(a: int, b: int) -> int:
await asyncio.sleep(0.1)
return a - b
@jsonrpc
async def test() -> str:
await asyncio.sleep(0.1)
return "中文"
if name == 'main':
app.run(host='127.0.0.1', port=8000)
`
Start the service and test that everything is OK!
Report the error of 'different loop' after stopping the service
it is as follows
`
[2020-02-26 18:39:29 +0800] [17605] [ERROR] Experienced exception while trying to serve
Traceback (most recent call last):
File "/home/guoyanfeng/virtual/aclients/lib/python3.6/site-packages/sanic/app.py", line 1167, in run
serve(**server_settings)
File "/home/guoyanfeng/virtual/aclients/lib/python3.6/site-packages/sanic/server.py", line 924, in serve
trigger_events(before_stop, loop)
File "/home/guoyanfeng/virtual/aclients/lib/python3.6/site-packages/sanic/server.py", line 667, in trigger_events
loop.run_until_complete(result)
File "uvloop/loop.pyx", line 1451, in uvloop.loop.Loop.run_until_complete
File "/home/guoyanfeng/virtual/aclients/lib/python3.6/site-packages/sanic_jsonrpc/jsonrpc.py", line 308, in finish_calls
await self._processing_task
File "/home/guoyanfeng/virtual/aclients/lib/python3.6/site-packages/sanic_jsonrpc/jsonrpc.py", line 271, in _processing
call = await calls.get()
File "/usr/lib/python3.6/asyncio/queues.py", line 167, in get
yield from getter
RuntimeError: Task <Task pending coro=<Jsonrpc._processing() running at /home/guoyanfeng/virtual/aclients/lib/python3.6/site-packages/sanic_jsonrpc/jsonrpc.py:271>> got Future attached to a different loop
`
After checking, I found that the loop obtained by queue is inconsistent with the app.
Modified code, test correct!
`
self._routes = {}
self._calls = None
self._processing_task = None
@app.listener('after_server_start')
async def start_processing(_app, _loop):
self._calls = Queue(loop=_loop)
self._processing_task = ensure_future(self._processing())
`
I hope the author can change it in the source code. Thank you
Hello,
Your project looks really interesting and we are checking it out to use it on our next project. However, first thing we noticed is that it doesn't support Python 3.9. Is there any plans on adding that support, expanding the documentation and potentially add more features to it in the future?
Thanks!
Hi @mon4ter !
I tested the latest version (0.2.1) after installation and found that there was a 'jsronrpc' package missing.
The packages of the setup file are as follows.
packages=[
'sanic_jsonrpc',
'sanic_jsonrpc._routing',
'sanic_jsonrpc.models',
],
missing 'sanic_jsonrpc.jsonrpc'
I will use version 0.1.1 first.
Thank you
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.