{"id":18621260,"url":"https://github.com/sofastack/sofa-bolt-python","last_synced_at":"2025-04-11T02:31:37.986Z","repository":{"id":47246454,"uuid":"146727213","full_name":"sofastack/sofa-bolt-python","owner":"sofastack","description":"The Python implementation of the SOFABolt protocol.","archived":false,"fork":false,"pushed_at":"2023-12-06T12:19:29.000Z","size":126,"stargazers_count":47,"open_issues_count":2,"forks_count":14,"subscribers_count":10,"default_branch":"master","last_synced_at":"2025-03-24T20:06:17.634Z","etag":null,"topics":["python","python3","sofa-bolt","sofastack"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sofastack.png","metadata":{"files":{"readme":"README.en.md","changelog":"HISTORY.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-08-30T09:29:06.000Z","updated_at":"2023-05-02T10:21:27.000Z","dependencies_parsed_at":"2022-09-26T17:40:37.776Z","dependency_job_id":null,"html_url":"https://github.com/sofastack/sofa-bolt-python","commit_stats":null,"previous_names":["alipay/sofa-bolt-python"],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sofastack%2Fsofa-bolt-python","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sofastack%2Fsofa-bolt-python/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sofastack%2Fsofa-bolt-python/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sofastack%2Fsofa-bolt-python/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sofastack","download_url":"https://codeload.github.com/sofastack/sofa-bolt-python/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248329596,"owners_count":21085565,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["python","python3","sofa-bolt","sofastack"],"created_at":"2024-11-07T04:10:05.718Z","updated_at":"2025-04-11T02:31:32.974Z","avatar_url":"https://github.com/sofastack.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# anthunder(a.k.a. sofa-bolt-python)\n\nanthunder(ant thunder) is a sofa-bolt library written in python. \nIt supports RPC calling via 'sofa-bolt + protobuf' protocol.\n\n## requirements\n\n- python3 \u003e= 3.5 (aio classes needs asyncio support)\n- mosn \u003e= 1.3 (to use with version \u003e= 0.6)\n- mosn \u003c 1.3 (to use with version \u003c 0.6)\n\n## roadmap\n\n- [x] bolt client(protobuf serialization)\n- [x] service discover via mosn (sofa servicemesh sidecar)\n- [x] bolt server(protobuf serialization)\n- [x] sofa-hessian(and other) serialization protocol support\n\n## Tutorial\n\n### As client (caller)\n0. Acquire `.proto` file\n1. Execute `protoc --python_out=. *.proto` to compile protobuf file, and get `_pb2.py` file.\n2. Import protobuf classes (postfixed with `_pb2`)\n\n```python\nfrom SampleServicePbResult_pb2 import SampleServicePbResult\nfrom SampleServicePbRequest_pb2 import SampleServicePbRequest\n\nfrom anthunder import AioClient\nfrom anthunder.discovery.mosn import MosnClient, ApplicationInfo\n\n\nspanctx = SpanContext()         # generate a new context, an object of mytracer.SpanContext, stores rpc_trace_context.\n# spanctx = ctx                 # or transfered from upstream rpc\nservice_reg = MosnClient()      # using mosn for service discovery, see https://mosn.io for detail\nservice_reg.startup(ApplicationInfo(YOUR_APP_NAME))\n# service_reg = LocalRegistry({interface: (inf_ip, inf_port)})  # or a service-address dict as service discovery\n\n# Subscribe interface before client requests\nservice_reg.subscribe(interface)\n\nclient = AioClient(YOUR_APP_NAME, service_register=service_reg) \n# will create a thread, and send heartbeat to remote every 30s\n\ninterface = 'com.alipay.rpc.common.service.facade.pb.SampleServicePb:1.0'\n\n\n# Call synchronously\ncontent = client.invoke_sync(interface, \"hello\",\n                             SampleServicePbRequest(name=some_name).SerializeToString(),\n                             timeout_ms=500, spanctx=spanctx)\nresult = SampleServicePbResult()\nresult.ParseFromString(content)\n\n# Call asynchronously\n\ndef client_callback(resp):\n    # callback function, accepts bytes as the only argument,\n    # then do deserialize and further processes\n    result = SampleServicePbResult()\n    result.ParseFromString(content)\n    # do something\n\nfuture = client.invoke_async(interface, \"hello\", \n                             SampleServicePbRequest(name=some_name).SerializeToString(),\n                             spanctx=spanctx, callback=client_callback)\n)\n\n```\n\nSee project's unittest for runnable demo\n\n### As server\n\n```python\nfrom anthunder.listener import aio_listener\nfrom anthunder.discovery.mosn import MosnClient, ApplicationInfo\n\n\nclass SampleService(object):\n    def __init__(self, ctx):\n        # service must accept one param as spanctx for rpc tracing support\n        self.ctx = ctx\n        \n    def hello(self, bs: bytes):\n        obj = SampleServicePbRequest()\n        obj.ParseFromString(bs)\n        print(\"Processing Request\", obj)\n        return SampleServicePbResult(result=obj.name).SerializeToString()\n\n\ninterface = 'com.alipay.rpc.common.service.facade.pb.SampleServicePb:1.0'\n\nservice_reg = MosnClient()      # using mosn for service discovery, see https://mosn.io for detail\nservice_reg.startup(ApplicationInfo(YOUR_APP_NAME))\nlistener = aio_listener.AioListener(('127.0.0.1', 12200), YOUR_APP_NAME, service_register=service_reg)\n# register interface and its function, plus its protobuf definition class\nlistener.register_interface(interface, service_cls=SampleService, provider_meta=ProviderMetaInfo(appName=\"test_app\"))\n# start server in a standalone thread\nlistener.run_threading()\n# or start in current thread\nlistener.run_forever()\n\n# publish interfaces, MUST after listener start.\nlistener.publish()\n\n# shutdown the server\nlistener.shutdown()\n\n```\n\n## License\n\nCopyright (c) 2018-present, Ant Financial Service Group\n\nApache License 2.0\n\nSee LICENSE file.\n\n## Thirdparty\n\nPart of the mysockpool package uses codes from [urllib3](https://github.com/urllib3/urllib3) project \nunder the term of MIT License. See origin-license.txt under the mysockpool package.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsofastack%2Fsofa-bolt-python","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsofastack%2Fsofa-bolt-python","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsofastack%2Fsofa-bolt-python/lists"}