Commit 6b478110 by lichenggang

classify

parents db354b83 e9424909
...@@ -9,16 +9,17 @@ from utils.predic_fac import PredictorFac ...@@ -9,16 +9,17 @@ from utils.predic_fac import PredictorFac
from utils.log_manager import get_logger from utils.log_manager import get_logger
log_server = get_logger('server') log_server = get_logger('server')
class Classify(classify_pb2_grpc.classifyServicer): class Classify(classify_pb2_grpc.classifyServicer):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
log_server.info('classify server start!') log_server.info('classify server start!')
self.predictorfac = PredictorFac(model_config) self.predictorfac = PredictorFac(model_config)
def SingleClassify(self, request, context): def Classify(self, request, context):
log_server.debug('接收数据: ' + request.keyword) log_server.debug('接收数据: ' + request.keyword)
res = self.predictorfac.predic(request.keyword) res = self.predictorfac.predic(request.keyword)
return classify_pb2.SingleClassifyReply(message='result {msg}'.format(msg=res)) return classify_pb2.ClassifyReply(message='result {msg}'.format(msg=res))
def fac_test_predic(self, data): def fac_test_predic(self, data):
res = self.predictorfac.predic(data) res = self.predictorfac.predic(data)
......
...@@ -8,7 +8,7 @@ def run(): ...@@ -8,7 +8,7 @@ def run():
channel = grpc.insecure_channel('localhost:50051') channel = grpc.insecure_channel('localhost:50051')
# 调用 rpc 服务 # 调用 rpc 服务
stub = classify_pb2_grpc.classifyStub(channel) stub = classify_pb2_grpc.classifyStub(channel)
response = stub.SingleClassify(classify_pb2.SingleClassifyRequest(keyword='czl')) response = stub.Classify(classify_pb2.ClassifyRequest(keyword='czl'))
print("Classify client received: " + response.message) print("Classify client received: " + response.message)
if __name__ == '__main__': if __name__ == '__main__':
......
// [python quickstart](https://grpc.io/docs/quickstart/python.html#run-a-grpc-application) // [python quickstart](https://grpc.io/docs/quickstart/python.html#run-a-grpc-application)
// python -m grpc_tools.protoc --python_out=. --grpc_python_out=. -I. helloworld.proto // python -m grpc_tools.protoc --python_out=. --grpc_python_out=. -I. classify.proto
// 选择类型 // 选择类型
syntax = "proto3"; syntax = "proto3";
service classify { service classify {
rpc SingleClassify(SingleClassifyRequest) returns (SingleClassifyReply) {} rpc Classify(ClassifyRequest) returns (ClassifyReply) {}
} }
message SingleClassifyRequest { message ClassifyRequest {
string keyword = 1; string keyword = 1;
} }
message SingleClassifyReply { message ClassifyReply {
string message = 1; string message = 1;
} }
......
...@@ -18,21 +18,21 @@ DESCRIPTOR = _descriptor.FileDescriptor( ...@@ -18,21 +18,21 @@ DESCRIPTOR = _descriptor.FileDescriptor(
package='', package='',
syntax='proto3', syntax='proto3',
serialized_options=None, serialized_options=None,
serialized_pb=b'\n\x0e\x63lassify.proto\"(\n\x15SingleClassifyRequest\x12\x0f\n\x07keyword\x18\x01 \x01(\t\"&\n\x13SingleClassifyReply\x12\x0f\n\x07message\x18\x01 \x01(\t2L\n\x08\x63lassify\x12@\n\x0eSingleClassify\x12\x16.SingleClassifyRequest\x1a\x14.SingleClassifyReply\"\x00\x62\x06proto3' serialized_pb=b'\n\x0e\x63lassify.proto\"\"\n\x0f\x43lassifyRequest\x12\x0f\n\x07keyword\x18\x01 \x01(\t\" \n\rClassifyReply\x12\x0f\n\x07message\x18\x01 \x01(\t2:\n\x08\x63lassify\x12.\n\x08\x43lassify\x12\x10.ClassifyRequest\x1a\x0e.ClassifyReply\"\x00\x62\x06proto3'
) )
_SINGLECLASSIFYREQUEST = _descriptor.Descriptor( _CLASSIFYREQUEST = _descriptor.Descriptor(
name='SingleClassifyRequest', name='ClassifyRequest',
full_name='SingleClassifyRequest', full_name='ClassifyRequest',
filename=None, filename=None,
file=DESCRIPTOR, file=DESCRIPTOR,
containing_type=None, containing_type=None,
fields=[ fields=[
_descriptor.FieldDescriptor( _descriptor.FieldDescriptor(
name='keyword', full_name='SingleClassifyRequest.keyword', index=0, name='keyword', full_name='ClassifyRequest.keyword', index=0,
number=1, type=9, cpp_type=9, label=1, number=1, type=9, cpp_type=9, label=1,
has_default_value=False, default_value=b"".decode('utf-8'), has_default_value=False, default_value=b"".decode('utf-8'),
message_type=None, enum_type=None, containing_type=None, message_type=None, enum_type=None, containing_type=None,
...@@ -51,19 +51,19 @@ _SINGLECLASSIFYREQUEST = _descriptor.Descriptor( ...@@ -51,19 +51,19 @@ _SINGLECLASSIFYREQUEST = _descriptor.Descriptor(
oneofs=[ oneofs=[
], ],
serialized_start=18, serialized_start=18,
serialized_end=58, serialized_end=52,
) )
_SINGLECLASSIFYREPLY = _descriptor.Descriptor( _CLASSIFYREPLY = _descriptor.Descriptor(
name='SingleClassifyReply', name='ClassifyReply',
full_name='SingleClassifyReply', full_name='ClassifyReply',
filename=None, filename=None,
file=DESCRIPTOR, file=DESCRIPTOR,
containing_type=None, containing_type=None,
fields=[ fields=[
_descriptor.FieldDescriptor( _descriptor.FieldDescriptor(
name='message', full_name='SingleClassifyReply.message', index=0, name='message', full_name='ClassifyReply.message', index=0,
number=1, type=9, cpp_type=9, label=1, number=1, type=9, cpp_type=9, label=1,
has_default_value=False, default_value=b"".decode('utf-8'), has_default_value=False, default_value=b"".decode('utf-8'),
message_type=None, enum_type=None, containing_type=None, message_type=None, enum_type=None, containing_type=None,
...@@ -81,27 +81,27 @@ _SINGLECLASSIFYREPLY = _descriptor.Descriptor( ...@@ -81,27 +81,27 @@ _SINGLECLASSIFYREPLY = _descriptor.Descriptor(
extension_ranges=[], extension_ranges=[],
oneofs=[ oneofs=[
], ],
serialized_start=60, serialized_start=54,
serialized_end=98, serialized_end=86,
) )
DESCRIPTOR.message_types_by_name['SingleClassifyRequest'] = _SINGLECLASSIFYREQUEST DESCRIPTOR.message_types_by_name['ClassifyRequest'] = _CLASSIFYREQUEST
DESCRIPTOR.message_types_by_name['SingleClassifyReply'] = _SINGLECLASSIFYREPLY DESCRIPTOR.message_types_by_name['ClassifyReply'] = _CLASSIFYREPLY
_sym_db.RegisterFileDescriptor(DESCRIPTOR) _sym_db.RegisterFileDescriptor(DESCRIPTOR)
SingleClassifyRequest = _reflection.GeneratedProtocolMessageType('SingleClassifyRequest', (_message.Message,), { ClassifyRequest = _reflection.GeneratedProtocolMessageType('ClassifyRequest', (_message.Message,), {
'DESCRIPTOR' : _SINGLECLASSIFYREQUEST, 'DESCRIPTOR' : _CLASSIFYREQUEST,
'__module__' : 'classify_pb2' '__module__' : 'classify_pb2'
# @@protoc_insertion_point(class_scope:SingleClassifyRequest) # @@protoc_insertion_point(class_scope:ClassifyRequest)
}) })
_sym_db.RegisterMessage(SingleClassifyRequest) _sym_db.RegisterMessage(ClassifyRequest)
SingleClassifyReply = _reflection.GeneratedProtocolMessageType('SingleClassifyReply', (_message.Message,), { ClassifyReply = _reflection.GeneratedProtocolMessageType('ClassifyReply', (_message.Message,), {
'DESCRIPTOR' : _SINGLECLASSIFYREPLY, 'DESCRIPTOR' : _CLASSIFYREPLY,
'__module__' : 'classify_pb2' '__module__' : 'classify_pb2'
# @@protoc_insertion_point(class_scope:SingleClassifyReply) # @@protoc_insertion_point(class_scope:ClassifyReply)
}) })
_sym_db.RegisterMessage(SingleClassifyReply) _sym_db.RegisterMessage(ClassifyReply)
...@@ -111,16 +111,16 @@ _CLASSIFY = _descriptor.ServiceDescriptor( ...@@ -111,16 +111,16 @@ _CLASSIFY = _descriptor.ServiceDescriptor(
file=DESCRIPTOR, file=DESCRIPTOR,
index=0, index=0,
serialized_options=None, serialized_options=None,
serialized_start=100, serialized_start=88,
serialized_end=176, serialized_end=146,
methods=[ methods=[
_descriptor.MethodDescriptor( _descriptor.MethodDescriptor(
name='SingleClassify', name='Classify',
full_name='classify.SingleClassify', full_name='classify.Classify',
index=0, index=0,
containing_service=None, containing_service=None,
input_type=_SINGLECLASSIFYREQUEST, input_type=_CLASSIFYREQUEST,
output_type=_SINGLECLASSIFYREPLY, output_type=_CLASSIFYREPLY,
serialized_options=None, serialized_options=None,
), ),
]) ])
......
...@@ -13,17 +13,17 @@ class classifyStub(object): ...@@ -13,17 +13,17 @@ class classifyStub(object):
Args: Args:
channel: A grpc.Channel. channel: A grpc.Channel.
""" """
self.SingleClassify = channel.unary_unary( self.Classify = channel.unary_unary(
'/classify/SingleClassify', '/classify/Classify',
request_serializer=classify__pb2.SingleClassifyRequest.SerializeToString, request_serializer=classify__pb2.ClassifyRequest.SerializeToString,
response_deserializer=classify__pb2.SingleClassifyReply.FromString, response_deserializer=classify__pb2.ClassifyReply.FromString,
) )
class classifyServicer(object): class classifyServicer(object):
"""Missing associated documentation comment in .proto file""" """Missing associated documentation comment in .proto file"""
def SingleClassify(self, request, context): def Classify(self, request, context):
"""Missing associated documentation comment in .proto file""" """Missing associated documentation comment in .proto file"""
context.set_code(grpc.StatusCode.UNIMPLEMENTED) context.set_code(grpc.StatusCode.UNIMPLEMENTED)
context.set_details('Method not implemented!') context.set_details('Method not implemented!')
...@@ -32,10 +32,10 @@ class classifyServicer(object): ...@@ -32,10 +32,10 @@ class classifyServicer(object):
def add_classifyServicer_to_server(servicer, server): def add_classifyServicer_to_server(servicer, server):
rpc_method_handlers = { rpc_method_handlers = {
'SingleClassify': grpc.unary_unary_rpc_method_handler( 'Classify': grpc.unary_unary_rpc_method_handler(
servicer.SingleClassify, servicer.Classify,
request_deserializer=classify__pb2.SingleClassifyRequest.FromString, request_deserializer=classify__pb2.ClassifyRequest.FromString,
response_serializer=classify__pb2.SingleClassifyReply.SerializeToString, response_serializer=classify__pb2.ClassifyReply.SerializeToString,
), ),
} }
generic_handler = grpc.method_handlers_generic_handler( generic_handler = grpc.method_handlers_generic_handler(
...@@ -48,7 +48,7 @@ class classify(object): ...@@ -48,7 +48,7 @@ class classify(object):
"""Missing associated documentation comment in .proto file""" """Missing associated documentation comment in .proto file"""
@staticmethod @staticmethod
def SingleClassify(request, def Classify(request,
target, target,
options=(), options=(),
channel_credentials=None, channel_credentials=None,
...@@ -57,8 +57,8 @@ class classify(object): ...@@ -57,8 +57,8 @@ class classify(object):
wait_for_ready=None, wait_for_ready=None,
timeout=None, timeout=None,
metadata=None): metadata=None):
return grpc.experimental.unary_unary(request, target, '/classify/SingleClassify', return grpc.experimental.unary_unary(request, target, '/classify/Classify',
classify__pb2.SingleClassifyRequest.SerializeToString, classify__pb2.ClassifyRequest.SerializeToString,
classify__pb2.SingleClassifyReply.FromString, classify__pb2.ClassifyReply.FromString,
options, channel_credentials, options, channel_credentials,
call_credentials, compression, wait_for_ready, timeout, metadata) call_credentials, compression, wait_for_ready, timeout, metadata)
...@@ -22,3 +22,5 @@ class PredictorFac(): ...@@ -22,3 +22,5 @@ class PredictorFac():
predictor = self._get_predictor(data) predictor = self._get_predictor(data)
res = predictor.predict(data) res = predictor.predict(data)
return res return res
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment