在grpc调用中使用时,Tensorflow模型服务不起作用

时间:2019-02-14 01:10:06

标签: python-3.x tensorflow grpc tensorflow-serving grpc-python

已使用grpc创建了rpc服务。当我尝试在grpc调用(预测)中调用张量流预测函数时,我遇到了与张量流相关的错误,发现tf.global_variables()为空。这是代码

class ModelServiceServicer(modelService_pb2_grpc.ModelServiceServicer):
"""Provides methods that implement functionality of route guide 
   server."""

def __init__(self, model_dir_path, metrics_enabled, metrics_service):
    self.metrics_enabled = metrics_enabled
    self.metrics_service = metrics_service
    self.serialized_model = TfContainer(model_dir_path)
    self.input = ["""#input"""]        
    **#this works**
    print(self.serialized_model.handle_predict(self.input))        
    print("intialized")
    print(tf.global_variables())


def predict(self, request, context):
    print("request is ")
    #print(request)
    print(context)
    start_time = datetime.now()
    print(self.input)
    print(self.serialized_model)
    try:
        **#this becomes empty**
        print(tf.global_variables())
        **#this does not work.**
        print(self.serialized_model.handle_predict(self.input))
    except:
        traceback.print_stack()
        print('------')
        traceback.print_exc()      **
    print(self.serialized_model.handle_predict(self.input))        
    print("intialized")
    print(tf.global_variables())  

我们将不胜感激!

1 个答案:

答案 0 :(得分:0)

实际的Servicer方法调用确实发生在另一个线程中,尤其是在初始化服务器时提供的ThreadPool中。根据TensorFlow document tf.global_variables似乎不直接绑定到线程,不像tf.local_variables。您能否提供有关tf.global_variables的期望值的更多信息?或者最小的可复制片段甚至更好!