python msgpack RPC over websocket
Project description
wsmpRPC
Python msgpack RPC over websocket
Features
One of the advantages of msgpack over json is that msgpack can carry binary data, that makes it very useful for streaming.
wsmpRPC implements similar functions to that of gRPC, supporting not only one-shot RPC, but also bidirectional streaming and cancellation, without the trouble to define .proto files, and it's asynchronous!
Install
pip install wsmprpc
Examples
server_test.py
import asyncio
from sanic import Sanic
from wsmprpc import RPCServer
class SimpleHandler:
# rpc method name cannot start with underscore
def _private_method(self):
pass
# non-async rpc can't be cancelled
def div(self, a, b):
return a / b
# long runing rpc must be async
async def sleep(self, t):
await asyncio.sleep(t)
return 'done sleeping'
# request-streaming rpc: the client sends a sequence of messages and the server returns one response msg.
# the function must take the last arg as a keyword argument named 'request_stream',
# which is a sub class of asyncio.Queue.
async def sum(self, *, request_stream):
sum = 0
async for a in request_stream:
sum += a
return sum
# response-streaming rpc: the client send one request msg and the server returns a sequence of messages.
# the function must be an async generator function.
async def repeat(self, word, count):
while count > 0:
count -= 1
yield word
# combine request-streaming and response-streaming
async def uppercase(self, *, request_stream):
async for word in request_stream:
yield word.upper()
app = Sanic(__name__)
@app.websocket("/")
async def home(request, ws):
await RPCServer(ws, SimpleHandler()).run()
app.run(host="0.0.0.0", port=8000)
client_test.py
import asyncio, websockets
from wsmprpc import RPCClient
async def main():
async with websockets.connect('ws://localhost:8000') as ws:
stub = RPCClient(ws)
# normal rpc
print(await stub.div(1, 3))
# cancellation
s = stub.sleep(3)
async def cancel_sleep():
await asyncio.sleep(1)
s.cancel()
try:
asyncio.create_task(cancel_sleep())
print(await s)
except asyncio.CancelledError as e:
print('cancelled')
# request-streaming
print(await stub.sum(request_stream=range(1, 3)))
# get response-streaming
async for i in stub.repeat('bla...', 4):
print(i)
# combine request-streaming and response-streaming
async for i in stub.uppercase(request_stream=['hello', 'rpc']):
print(i)
asyncio.run(main())
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
wsmprpc-0.1.tar.gz
(6.4 kB
view details)
Built Distribution
wsmprpc-0.1-py3-none-any.whl
(6.2 kB
view details)
File details
Details for the file wsmprpc-0.1.tar.gz
.
File metadata
- Download URL: wsmprpc-0.1.tar.gz
- Upload date:
- Size: 6.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.6.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 23f06ec587b213e4c692f8ece7ce2825e7d5528e7667603161d38e5a8cbb0c3c |
|
MD5 | c1e41b18db4b9f4098871e89181b664b |
|
BLAKE2b-256 | 0c883485e8ca90fcdb6b813bfa1ea6a484338a26fc23d6c59ad9eff62af1dc51 |
File details
Details for the file wsmprpc-0.1-py3-none-any.whl
.
File metadata
- Download URL: wsmprpc-0.1-py3-none-any.whl
- Upload date:
- Size: 6.2 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/39.0.1 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.6.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f0ff8f92f6e64eddb93d51261e3d53f9d344f90a2a3d11d1088d657e16238975 |
|
MD5 | 1e29bd09172dc08a80f0364fdc8b95dd |
|
BLAKE2b-256 | 2719dd783ed3b41f441f7e6031501d043824297c85f0b93909a0d7e5b3450b74 |