An aio RPC framework based on RabbitMQ.
Project description
甲马 jiama
一个基于 RabbitMQ 的异步 RPC 框架。 An asyncio RPC framework based on RabbitMQ.
俗话说:外事不决用 REST,内事不决用 RPC,找一圈只发现一个 Nameko,却是同步的,遂有 Jiama 问世。
安装 install
pip install jiama
Rabbitmq 的安装可以使用 docker 方式,具体参见官网。
sudo docker run -d --name rabbitmq -p 5672:5672 -p 15672:15672 rabbitmq:3.10-management
接口 API
jiama run module_path:ServiceClass -c config.toml
jiama run 是一个 Shell 命令,用于启动远程服务,后跟多个模块路径和服务类作为参数,用 -c 指定配置文件。
jiama.server.rpc
服务端装饰器,用于标志一个方法为远程服务方法。
jiama.client.RpcProxy()
RPC 服务代理,单例模式,提供在客户端访问远程服务的能力。
jiama.client.RpcProxy().create(config: dict)
创建 RPC 服务代理
configdict - 配置选项,包括 RPC 和 log 配置,具体参见 test/jiama/config.toml
jiama.client.RpcProxy.close()
关闭远程服务代理
rpc.service.method()
访问远程服务方法
示例 examples
服务端
jiama/sample.py
from jiama.client import RpcProxy
from jiama.server import rpc
class Service1:
@rpc
def add(self, x=1, y=2):
return x + y
class Service2:
@rpc
def sub(self, x=10, y=1):
return x - y
class Service3:
def __init__(self):
self.config = {'uri': 'amqp://guest:guest@localhost'}
@rpc
async def mul(self, x=1):
async with await RpcProxy().create(self.config) as rpc:
y = await rpc.Service1.add(1, 1)
return x * y
jiama run jiama.sample -c ./config.toml
客户端
import asyncio
from jiama.client import RpcProxy
class Client:
def __init__(self):
self.config = {
'rpc': {
'client_id': 'test',
'amqp_uri': 'amqp://guest:guest@localhost/',
}
}
async def init(self):
'''
这是一个需要被你的框架自动调用的初始化方法,比如: fastAPI 的 startup
This is a initialization method invoked by you framework like fastAPI's startup
'''
self.rpc = await RpcProxy().create(self.config)
return self
async def req(self):
r = await self.rpc.Service1.add(3, 2)
logger.info(f'Result of add is {r}')
r = await self.rpc.Service2.sub(30, 9)
logger.info(f'Result of sub is {r}')
r = await self.rpc.Service3.mul(5)
logger.info(f'Result of mul with nested rpc is {r}')
async def destroy(self):
await self.rpc.close()
async def main():
c = await Client().init()
await c.req()
await c.destroy()
if __name__ == '__main__':
asyncio.run(main())
License
MIT © Li zhigang
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file jiama-0.8.1.tar.gz.
File metadata
- Download URL: jiama-0.8.1.tar.gz
- Upload date:
- Size: 11.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.10.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
37dc7cb95085bcdc98ac9fa5d8d66b813ca04cb4980db8966d064d8702f013e3
|
|
| MD5 |
1978f7a613b35ac820c5a38a8ce82f55
|
|
| BLAKE2b-256 |
3cdf5fd8fdfac97fd7dfe42ec95402208be91f7bb7f6617e603616a8de0bfc4e
|
File details
Details for the file jiama-0.8.1-py3-none-any.whl.
File metadata
- Download URL: jiama-0.8.1-py3-none-any.whl
- Upload date:
- Size: 11.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.10.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0a9ed817f80a7e3ccc183e65f4d877ad4fe16e673ffe2338cdc4a7454fef980d
|
|
| MD5 |
1340d0829eebf9aa80e51041b3843c51
|
|
| BLAKE2b-256 |
78f051fcd5ff93dae8ddf9b1276712464c378c02ad75c61110195648e46a0d90
|