def run(): # 1.使用上下文管理器,指定连接的rpc服务器的主机和端口,创建channel对象 with grpc.insecure_channel('127.0.0.1:8888') as channel: # 2.使用reco_pb2_grpc里面的stub工具,传入channel stub = reco_pb2_grpc.UserRecommendStub(channel) # 3.定义方法,传入stub工具,用来发送请求,解析响应 feed_articles(stub)
def run(): # 1、通过上下文连接rpc服务端 with grpc.insecure_channel('127.0.0.1:8888') as channel: # 2、实例化stub stub = reco_pb2_grpc.UserRecommendStub(channel) # 3、通过stub进行对rpc调用获取结果 rec_response = get_grpc_func_ret(stub) return rec_response
def run(): # 1.通过上下文环境链接rpc服务端 with grpc.insecure_channel('127.0.0.1:8888') as channel: # 2.实例化stub对象 stub = reco_pb2_grpc.UserRecommendStub(channel) # 3.通过stub调用远端函数获取结果 rpc_response = get_grpc_func_ret(stub) return rpc_response
def run(): # 构建连接rpc服务器的对象 with grpc.insecure_channel('127.0.0.1:8888') as channel: # 创建调用的辅助工具对象 stub stub = reco_pb2_grpc.UserRecommendStub(channel) # 可以通过stub进行rpc调用 # ret = stub.user_recommend() feed_articles(stub)
def run(): """ rpc客户端调用的方法 """ # 使用with语句连接rpc服务器 with grpc.insecure_channel('127.0.0.1:8888') as channel: # 创建调用rpc远端服务的辅助对象stub stub = reco_pb2_grpc.UserRecommendStub(channel) # 通过stub进行rpc调用 feed_articles(stub)
def run(): # 创建与服务器的连接,用with语句管理,拿到渠道channek with grpc.insecure_channel('127.0.0.1:8888') as channel: # 构建辅助调用的工具,必须传入连接渠道 stub = reco_pb2_grpc.UserRecommendStub(channel) # 进行rpc调用 req = reco_pb2.UserRequest() req.user_id = '1' req.channel_id = 12 req.article_num = 10 req.time_stamp = round(time.time() * 1000) ret = stub.user_recommend(req) print(ret)
def run(): """ 客户端调用运行 :return: """ # 与rpc服务器建立连接 with grpc.insecure_channel('127.0.0.1:8888') as channel: # 创建用户进行rpc调用的工具 stub = reco_pb2_grpc.UserRecommendStub(channel) # 进行rpc调用 req = reco_pb2.UserRequest() req.user_id = '1' req.channel_id = 12 req.article_num = 10 req.time_stamp = round(time.time() * 1000) ret = stub.user_recommend(req) # ret -> ArticleResponse 对象 print('ret={}'.format(ret))