Commit 6b478110 by lichenggang

classify

parents db354b83 e9424909
......@@ -9,16 +9,17 @@ from utils.predic_fac import PredictorFac
from utils.log_manager import get_logger
log_server = get_logger('server')
class Classify(classify_pb2_grpc.classifyServicer):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
log_server.info('classify server start!')
self.predictorfac = PredictorFac(model_config)
def SingleClassify(self, request, context):
def Classify(self, request, context):
log_server.debug('接收数据: ' + 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):
res = self.predictorfac.predic(data)
......
......@@ -8,7 +8,7 @@ def run():
channel = grpc.insecure_channel('localhost:50051')
# 调用 rpc 服务
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)
if __name__ == '__main__':
......
// [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";
service classify {
rpc SingleClassify(SingleClassifyRequest) returns (SingleClassifyReply) {}
rpc Classify(ClassifyRequest) returns (ClassifyReply) {}
}
message SingleClassifyRequest {
message ClassifyRequest {
string keyword = 1;
}
message SingleClassifyReply {
message ClassifyReply {
string message = 1;
}
......
......@@ -18,21 +18,21 @@ DESCRIPTOR = _descriptor.FileDescriptor(
package='',
syntax='proto3',
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(
name='SingleClassifyRequest',
full_name='SingleClassifyRequest',
_CLASSIFYREQUEST = _descriptor.Descriptor(
name='ClassifyRequest',
full_name='ClassifyRequest',
filename=None,
file=DESCRIPTOR,
containing_type=None,
fields=[
_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,
has_default_value=False, default_value=b"".decode('utf-8'),
message_type=None, enum_type=None, containing_type=None,
......@@ -51,19 +51,19 @@ _SINGLECLASSIFYREQUEST = _descriptor.Descriptor(
oneofs=[
],
serialized_start=18,
serialized_end=58,
serialized_end=52,
)
_SINGLECLASSIFYREPLY = _descriptor.Descriptor(
name='SingleClassifyReply',
full_name='SingleClassifyReply',
_CLASSIFYREPLY = _descriptor.Descriptor(
name='ClassifyReply',
full_name='ClassifyReply',
filename=None,
file=DESCRIPTOR,
containing_type=None,
fields=[
_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,
has_default_value=False, default_value=b"".decode('utf-8'),
message_type=None, enum_type=None, containing_type=None,
......@@ -81,27 +81,27 @@ _SINGLECLASSIFYREPLY = _descriptor.Descriptor(
extension_ranges=[],
oneofs=[
],
serialized_start=60,
serialized_end=98,
serialized_start=54,
serialized_end=86,
)
DESCRIPTOR.message_types_by_name['SingleClassifyRequest'] = _SINGLECLASSIFYREQUEST
DESCRIPTOR.message_types_by_name['SingleClassifyReply'] = _SINGLECLASSIFYREPLY
DESCRIPTOR.message_types_by_name['ClassifyRequest'] = _CLASSIFYREQUEST
DESCRIPTOR.message_types_by_name['ClassifyReply'] = _CLASSIFYREPLY
_sym_db.RegisterFileDescriptor(DESCRIPTOR)
SingleClassifyRequest = _reflection.GeneratedProtocolMessageType('SingleClassifyRequest', (_message.Message,), {
'DESCRIPTOR' : _SINGLECLASSIFYREQUEST,
ClassifyRequest = _reflection.GeneratedProtocolMessageType('ClassifyRequest', (_message.Message,), {
'DESCRIPTOR' : _CLASSIFYREQUEST,
'__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,), {
'DESCRIPTOR' : _SINGLECLASSIFYREPLY,
ClassifyReply = _reflection.GeneratedProtocolMessageType('ClassifyReply', (_message.Message,), {
'DESCRIPTOR' : _CLASSIFYREPLY,
'__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(
file=DESCRIPTOR,
index=0,
serialized_options=None,
serialized_start=100,
serialized_end=176,
serialized_start=88,
serialized_end=146,
methods=[
_descriptor.MethodDescriptor(
name='SingleClassify',
full_name='classify.SingleClassify',
name='Classify',
full_name='classify.Classify',
index=0,
containing_service=None,
input_type=_SINGLECLASSIFYREQUEST,
output_type=_SINGLECLASSIFYREPLY,
input_type=_CLASSIFYREQUEST,
output_type=_CLASSIFYREPLY,
serialized_options=None,
),
])
......
......@@ -13,17 +13,17 @@ class classifyStub(object):
Args:
channel: A grpc.Channel.
"""
self.SingleClassify = channel.unary_unary(
'/classify/SingleClassify',
request_serializer=classify__pb2.SingleClassifyRequest.SerializeToString,
response_deserializer=classify__pb2.SingleClassifyReply.FromString,
self.Classify = channel.unary_unary(
'/classify/Classify',
request_serializer=classify__pb2.ClassifyRequest.SerializeToString,
response_deserializer=classify__pb2.ClassifyReply.FromString,
)
class classifyServicer(object):
"""Missing associated documentation comment in .proto file"""
def SingleClassify(self, request, context):
def Classify(self, request, context):
"""Missing associated documentation comment in .proto file"""
context.set_code(grpc.StatusCode.UNIMPLEMENTED)
context.set_details('Method not implemented!')
......@@ -32,10 +32,10 @@ class classifyServicer(object):
def add_classifyServicer_to_server(servicer, server):
rpc_method_handlers = {
'SingleClassify': grpc.unary_unary_rpc_method_handler(
servicer.SingleClassify,
request_deserializer=classify__pb2.SingleClassifyRequest.FromString,
response_serializer=classify__pb2.SingleClassifyReply.SerializeToString,
'Classify': grpc.unary_unary_rpc_method_handler(
servicer.Classify,
request_deserializer=classify__pb2.ClassifyRequest.FromString,
response_serializer=classify__pb2.ClassifyReply.SerializeToString,
),
}
generic_handler = grpc.method_handlers_generic_handler(
......@@ -48,7 +48,7 @@ class classify(object):
"""Missing associated documentation comment in .proto file"""
@staticmethod
def SingleClassify(request,
def Classify(request,
target,
options=(),
channel_credentials=None,
......@@ -57,8 +57,8 @@ class classify(object):
wait_for_ready=None,
timeout=None,
metadata=None):
return grpc.experimental.unary_unary(request, target, '/classify/SingleClassify',
classify__pb2.SingleClassifyRequest.SerializeToString,
classify__pb2.SingleClassifyReply.FromString,
return grpc.experimental.unary_unary(request, target, '/classify/Classify',
classify__pb2.ClassifyRequest.SerializeToString,
classify__pb2.ClassifyReply.FromString,
options, channel_credentials,
call_credentials, compression, wait_for_ready, timeout, metadata)
......@@ -22,3 +22,5 @@ class PredictorFac():
predictor = self._get_predictor(data)
res = predictor.predict(data)
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