kiwipy rpc模式简单试用

主要是对于功能的一个体验,测试几个功能

rpc模式代码

注意依赖了amqp协议,需要先部署服务

  • rabbitmq 服务
services:
  rabbitmq:
    image: rabbitmq:3-management
    ports:
      - "5672:5672"
      - "15672:15672"
    environment:
    - RABBITMQ_DEFAULT_USER=dalong
    - RABBITMQ_DEFAULT_PASS=dalong123
  • client.py
import kiwipy

with kiwipy.connect('amqp://dalong:dalong123@127.0.0.1/') as comm:
    # Send an RPC message
    response = comm.rpc_send('fib', 30).result()
    print((" [.] Got %r" % response))
    datas = [1,2]
    response = comm.rpc_send('msg', msg={"name":"dalong"}).result()
    print((" [.] Got %r" % response))
  • server.py

定义了两个方法,fib 以及msg,注意第一个参数应该是comm,实际上就是连接

import threading
import kiwipy

def fib(comm, num):
    if num == 0:
        return 0
    if num == 1:
        return 1

    return fib(comm, num - 1) + fib(comm, num - 2)

def msg(comm,datas):
    print("add",comm,datas)
    return dict(datas)
     
with kiwipy.connect('amqp://dalong:dalong123@127.0.0.1/') as comm:
    # Register an RPC subscriber with the name 'fib'
    comm.add_rpc_subscriber(fib, 'fib')
    comm.add_rpc_subscriber(msg, 'msg')
    # Now wait indefinitely for fibonacci calls
    threading.Event().wait()

运行效果

需要启动rabbitmq,同时先启动server,之后是client

  • 启动
docker-compose up -d
python serever.py
python client.py

效果

说明

kiwipy rpc模式对于参数的传递,推荐基于字典,目前测试对于其他多参数的支持会有问题,也可能是使用的问题,后边在研究下

参考资料

https://kiwipy.readthedocs.io/en/latest/

https://github.com/aiidateam/kiwipy

posted on 2025-03-11 08:00  荣锋亮  阅读(10)  评论(0)    收藏  举报

导航