基于ZeroMQ的高性能分布式RPC框架:Zerorpc
jopen
10年前
Zerorpc是一个基于ZeroMQ和MessagePack开发的远程过程调用协议(RPC)实现。和 Zerorpc 一起使用的 Service API 被称为 zeroservice。Zerorpc 可以通过编程或命令行方式调用。
它允许你:
- 不用修改代码即可显露python模块
- 通过命令行远程调用这些模块
如何把你代码中的对象暴露为一个zeroservice?
class Cooler(object): """ Various convenience methods to make things cooler. """ def add_man(self, sentence): """ End a sentence with ", man!" to make it sound cooler, and return the result. """ return sentence + ", man!" def add_42(self, n): """ Add 42 to an integer argument to make it cooler, and return the result. """ return n + 42 def boat(self, sentence): """ Replace a sentence with "I'm on a boat!", and return that, because it's cooler. """ return "I'm on a boat!" import zerorpc s = zerorpc.Server(Cooler()) s.bind("tcp://0.0.0.0:4242") s.run()
运行以上代码,在另一个终端,尝试连接这个zeroservice
$ zerorpc -j tcp://localhost:4242 add_42 1 43 $ zerorpc tcp://localhost:4242 add_man 'I own a mint-condition Volkswagen Golf' "I own a mint-condition Volkswagen Golf, man!" $ zerorpc tcp://localhost:4242 boat 'I own a mint-condition Volkswagen Golf, man!' "I'm on a boat!"