rpc框架thrift

时间:2025-01-09 16:07:02

跨语言的rpc框架

新建一个thrift文件

# ping service demo
service PingService {
string ping(), ping函数的返回类型是字符串
} server.py
# -*- coding: utf-8 -*-

import thriftpy

from thriftpy.rpc import make_server

pp_thrift = thriftpy.load("pingpong.thrift", module_name="pp_thrift")

class Dispatcher(object):
def ping(self):
print("ping pong!")
return 'pong' def main():
server = make_server(pp_thrift.PingService, Dispatcher(),
'0.0.0.0', 6000)
print("serving...")
server.serve() if __name__ == '__main__':
main()

client.py

# -*- coding: utf-8 -*-

import thriftpy

from thriftpy.rpc import client_context

pp_thrift = thriftpy.load("pingpong.thrift", module_name="pp_thrift")

def main():
with client_context(pp_thrift.PingService, '127.0.0.1', 6000) as c:
pong = c.ping()
print(pong) if __name__ == '__main__':
main()