def feed_article(stub): # 构建rpc调用的调用参数 user_request = reco_pb2.UserRequest() user_request.user_id = '1' user_request.channel_id = 1 user_request.article_num = 10 user_request.time_stamp = round(time.time() * 1000)
def _get_rpc_article_list(self, stub, user_id, channel_id:int, time_stamp=round(time.time()*1000), article_num=10): user_requset = reco_pb2.UserRequest() print(type(user_id), user_id) user_requset.user_id = str(user_id) user_requset.channel_id = int(channel_id) user_requset.time_stamp = time_stamp user_requset.article_num = article_num # 通过参数stub调用远程服务端的函数,获取返回的响应对象 rpc_response = stub.user_recommend(user_requset) print(rpc_response) print(rpc_response.recommends[0].track.read) return rpc_response
def _feed_articles(self, channel_id, timestamp, feed_count): # 连接grpc服务器 channel = current_app.rpc_reco_channel # 获取stub对象 stub = reco_pb2_grpc.UserArticleRecommendStub(channel) # 组织请求对象 request = reco_pb2.UserRequest() # 首页不要求用户强制登录 没有登录设置匿名用户: "annoy" request.user_id = str(g.user_id) if g.user_id else "annoy" request.channel_id = channel_id request.article_num = feed_count request.time_stamp = timestamp article_response = stub.user_recommend(request) return article_response.recommends, article_response.time_stamp
def _feed_articles(self, channel_id, timestamp, feed_count): """ 获取推荐文章 :param channel_id: 频道id :param feed_count: 推荐数量 :param timestamp: 时间戳 :return: [{article_id, trace_params}, ...], timestamp """ user_request = reco_pb2.UserRequest() user_request.user_id = str(g.user_id) if g.user_id else 'anony' user_request.channel_id = channel_id user_request.article_num = feed_count user_request.time_stamp = timestamp stub = reco_pb2_grpc.UserRecommendStub(current_app.rpc_reco_channel) ret = stub.user_recommend(user_request) # ret -> ArticleResponse 对象 exposure = ret.exposure pre_timestamp = ret.time_stamp recommends = ret.recommends return recommends, pre_timestamp
def _feed_articles(self, channel_id, timestamp, feed_count): """ rpc调用获取推荐文章 :param channel_id: 频道id :param feed_count: 推荐数量 :param timestamp: 时间戳 :return: [{article_id, trace_params}, ...], timestamp """ # 创建用户进行rpc调用的工具 stub = reco_pb2_grpc.UserRecommendStub(current_app.rpc_reco) # 进行rpc调用 req = reco_pb2.UserRequest() req.user_id = str(g.user_id) if g.user_id else 'Anonymous' req.channel_id = channel_id req.article_num = feed_count req.time_stamp = timestamp ret = stub.user_recommend(req) # ret -> ArticleResponse 对象 pre_timestamp = ret.time_stamp feed_articles = ret.recommends return feed_articles, pre_timestamp
def _feed_articles(self, channel_id, timestamp, feed_count): """ 获取推荐文章 :param channel_id: 频道id :param feed_count: 推荐数量 :param timestamp: 时间戳 :return: [{article_id, trace_params}, ...], timestamp """ #构建辅助调用工具 stub = reco_pb2_grpc.UserRecommendStub(current_app.rpc_reco) #进行rpc调用 user_request = reco_pb2.UserRequest() # userid用字符串的原因,有匿名用户,不登录的 user_request.user_id = g.user_id or 'annoy' user_request.channel_id = channel_id # 频道 user_request.article_num = feed_count # 文章数量 user_request.time_stamp = round(time.time() * 1000) # 时间戳 #feeds = user_request.recommend #pre_timwstamp = user_request.time_stamp ret = stub.user_recommend(user_request) # ret->ArticleResponse 对象 return ret.recommends, ret.time_stamp