anthunder(a.k.a. sofa-bolt-python)
anthunder(ant thunder) is a sofa-bolt library written in python.
It supports RPC calling via 'sofa-bolt + protobuf' protocol.
requirements
- python3 >= 3.5 (aio classes needs asyncio support)
- python2.7 (limited support, needs extra 3rd party libraries)
- mosn >= 1.3 (to use with version >= 0.6)
- mosn < 1.3 (to use with version < 0.6)
roadmap
Tutorial
As client (caller)
- Acquire
.proto
file
- Execute
protoc --python_out=. *.proto
to compile protobuf file, and get _pb2.py
file.
- Import protobuf classes (postfixed with
_pb2
)
from SampleServicePbResult_pb2 import SampleServicePbResult
from SampleServicePbRequest_pb2 import SampleServicePbRequest
from anthunder import AioClient
spanctx = SpanContext()
client = AioClient(my_app_name)
interface = 'com.alipay.rpc.common.service.facade.pb.SampleServicePb:1.0'
client.subscribe(interface)
content = client.invoke_sync(interface, "hello",
SampleServicePbRequest(name=some_name).SerializeToString(),
timeout_ms=500, spanctx=spanctx)
result = SampleServicePbResult()
result.ParseFromString(content)
def client_callback(resp):
result = SampleServicePbResult()
result.ParseFromString(content)
future = client.invoke_async(interface, "hello",
SampleServicePbRequest(name=some_name).SerializeToString(),
spanctx=spanctx, callback=client_callback)
)
See project's unittest for runnable demo
As server
from anthunder.listener import aio_listener
class SampleService(object):
def __init__(self, ctx):
self.ctx = ctx
def hello(self, bs: bytes):
obj = SampleServicePbRequest()
obj.ParseFromString(bs)
print("Processing Request", obj)
return SampleServicePbResult(result=obj.name).SerializeToString()
listener = aio_listener.AioListener(('127.0.0.1', 12200), "test_app")
listener.handler.register_interface("com.alipay.rpc.common.service.facade.pb.SampleServicePb:1.0",
SampleService)
listener.run_threading()
listener.run_forever()
listener.publish()
listener.shutdown()
License
Copyright (c) 2018-present, Ant Financial Service Group
Apache License 2.0
See LICENSE file.
Thirdparty
Part of the mysockpool package uses codes from urllib3 project
under the term of MIT License. See origin-license.txt under the mysockpool package.
Release History
0.5.6 (2019-03-15)
Bugfixes
- fix a infinite loop bug when parsing protocol
0.5.4 (2018-11-09)
Bugfixes
- fix server errors under python2.7
0.5.3 (2018-08-27)
Feature
- support antsharecloud parameters.
0.5.2 (2018-09-03)
Bugfixes
- fix various errors under python2.7
0.5.1 (2018-08-31)
Bugfixes
- sofa trace rpc id may contains str.