问题描述
Ubutun 18,python 3.6,gRPC 1.24.3
server.py:
import time
import grpc
from concurrent import futures
import tensorflow as tf
from google.protobuf import any_pb2
import numpy as np
import random
import compute_pb2,compute_pb2_grpc
class ComputeServicer(compute_pb2_grpc.ComputeServicer):
def SayHello(self,request,ctx):
max_len = str(len(request.helloworld))
return compute_pb2.HelloReply(result=max_len)
def Compute(self,ctx):
n = 100
# This 2 lines makes memory leak
for i in range(n):
value = tf.constant([str(random.randint(10000000,99999999)) * 16] * 5000000,dtype=tf.string)
'''
for i in range(n):
value = tf.constant([i] * 100000000,dtype=tf.int64)
'''
value = tf.constant([1] * 50000000,dtype=tf.int64)
tensor_proto = tf.make_tensor_proto(value)
any_pb = any_pb2.Any()
any_pb.Pack(tensor_proto)
value_proto = compute_pb2.Value(tensor=any_pb)
return compute_pb2.ComputeResponse(value=value_proto)
def main():
options = [
('grpc.max_send_message_length',-1),('grpc.max_receive_message_length',-1)
]
server_kwargs = {}
server_kwargs['options'] = options
server = grpc.server(futures.ThreadPoolExecutor(max_workers=10),**server_kwargs)
servicer = ComputeServicer()
compute_pb2_grpc.add_ComputeServicer_to_server(servicer,server)
server.add_insecure_port('127.0.0.1:19999')
server.start()
try:
print("running...")
time.sleep(1000)
except KeyboardInterrupt:
print("stopping...")
server.stop(0)
if __name__ == '__main__':
main()
client.py
import grpc
import compute_pb2
import compute_pb2_grpc
import tensorflow as tf
import time
_HOST = '127.0.0.1'
_PORT = '19999'
def main():
num = 0
while num < 100:
with grpc.insecure_channel("{0}:{1}".format(_HOST,_PORT),options=[('grpc.max_send_message_length',-1)]
) as channel:
client = compute_pb2_grpc.ComputeStub(channel=channel)
response = client.Compute(compute_pb2.HelloRequest(helloworld="123456"))
value_proto = response.value
tensor_proto = tf.make_tensor_proto(values=0)
if not value_proto.tensor.Unpack(tensor_proto):
raise ValueError('Unable to unpack the received tensor value.')
tensor_value = tf.make_ndarray(tensor_proto)
print(num)
num+=1
if __name__ == '__main__':
main()
compute.proto
Syntax = "proto3";
import "google/protobuf/any.proto";
package compute;
service Compute {
rpc SayHello (HelloRequest) returns (HelloReply) {}
rpc Compute(HelloRequest) returns (ComputeResponse) {}
}
message HelloRequest {
string helloworld = 1;
}
message HelloReply {
string result = 1;
}
message ComputeResponse {
Value value = 1;
}
message Value {
oneof value {
google.protobuf.Any tensor = 1;
}
}
python -m grpc_tools.protoc -I ./ --python_out =。/ --grpc_python_out =。/ compute.proto
python server.py
python client.py
使用上述运行命令时,服务器端的内存使用量会随着时间增加。如果我删除服务器中的字符串张量或将其替换为int64(请参阅我的注释),则服务器的内存使用情况是稳定的。
谢谢。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)