Commit d57eb5a7 by lzzzzl

Initial commit

parents
<component name="InspectionProjectProfileManager">
<settings>
<option name="useProjectProfile" value="false" />
<option name="USE_PROJECT_PROFILE" value="false" />
<version value="1.0" />
</settings>
</component>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.6.5 (C:\Program Files\python3\python.exe)" project-jdk-type="Python SDK" />
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/rpc.iml" filepath="$PROJECT_DIR$/.idea/rpc.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
<component name="TestRunnerService">
<option name="PROJECT_TEST_RUNNER" value="Unittests" />
</component>
</module>
\ No newline at end of file
import grpc
from protobuf import classify_pb2
from protobuf import classify_pb2_grpc
def run():
# 连接 rpc 服务器
channel = grpc.insecure_channel('localhost:50051')
# 调用 rpc 服务
stub = classify_pb2_grpc.classifyStub(channel)
response = stub.SingleClassify(classify_pb2.SingleClassifyRequest(keyword='czl'))
print("Classify client received: " + response.message)
if __name__ == '__main__':
run()
No preview for this file type
#!/usr/bin/env python
# -*- coding:utf-8 -*-
from sklearn.externals import joblib
import pandas as pd
import pickle
class KwPredict:
def PredictKeyword(self, keyword):
with open('extractor', 'rb') as f:
extractor = pickle.load(f)
classifier = joblib.load('model')
kw_series = pd.Series(keyword)
feature = extractor.transform(kw_series)
predictions = classifier.predict(feature)
return predictions
No preview for this file type
// [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
// 选择类型
syntax = "proto3";
service classify {
rpc SingleClassify(SingleClassifyRequest) returns (SingleClassifyReply) {}
}
message SingleClassifyRequest {
string keyword = 1;
}
message SingleClassifyReply {
string message = 1;
}
# -*- coding: utf-8 -*-
# Generated by the protocol buffer compiler. DO NOT EDIT!
# source: classify.proto
from google.protobuf import descriptor as _descriptor
from google.protobuf import message as _message
from google.protobuf import reflection as _reflection
from google.protobuf import symbol_database as _symbol_database
# @@protoc_insertion_point(imports)
_sym_db = _symbol_database.Default()
DESCRIPTOR = _descriptor.FileDescriptor(
name='classify.proto',
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'
)
_SINGLECLASSIFYREQUEST = _descriptor.Descriptor(
name='SingleClassifyRequest',
full_name='SingleClassifyRequest',
filename=None,
file=DESCRIPTOR,
containing_type=None,
fields=[
_descriptor.FieldDescriptor(
name='keyword', full_name='SingleClassifyRequest.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,
is_extension=False, extension_scope=None,
serialized_options=None, file=DESCRIPTOR),
],
extensions=[
],
nested_types=[],
enum_types=[
],
serialized_options=None,
is_extendable=False,
syntax='proto3',
extension_ranges=[],
oneofs=[
],
serialized_start=18,
serialized_end=58,
)
_SINGLECLASSIFYREPLY = _descriptor.Descriptor(
name='SingleClassifyReply',
full_name='SingleClassifyReply',
filename=None,
file=DESCRIPTOR,
containing_type=None,
fields=[
_descriptor.FieldDescriptor(
name='message', full_name='SingleClassifyReply.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,
is_extension=False, extension_scope=None,
serialized_options=None, file=DESCRIPTOR),
],
extensions=[
],
nested_types=[],
enum_types=[
],
serialized_options=None,
is_extendable=False,
syntax='proto3',
extension_ranges=[],
oneofs=[
],
serialized_start=60,
serialized_end=98,
)
DESCRIPTOR.message_types_by_name['SingleClassifyRequest'] = _SINGLECLASSIFYREQUEST
DESCRIPTOR.message_types_by_name['SingleClassifyReply'] = _SINGLECLASSIFYREPLY
_sym_db.RegisterFileDescriptor(DESCRIPTOR)
SingleClassifyRequest = _reflection.GeneratedProtocolMessageType('SingleClassifyRequest', (_message.Message,), {
'DESCRIPTOR' : _SINGLECLASSIFYREQUEST,
'__module__' : 'classify_pb2'
# @@protoc_insertion_point(class_scope:SingleClassifyRequest)
})
_sym_db.RegisterMessage(SingleClassifyRequest)
SingleClassifyReply = _reflection.GeneratedProtocolMessageType('SingleClassifyReply', (_message.Message,), {
'DESCRIPTOR' : _SINGLECLASSIFYREPLY,
'__module__' : 'classify_pb2'
# @@protoc_insertion_point(class_scope:SingleClassifyReply)
})
_sym_db.RegisterMessage(SingleClassifyReply)
_CLASSIFY = _descriptor.ServiceDescriptor(
name='classify',
full_name='classify',
file=DESCRIPTOR,
index=0,
serialized_options=None,
serialized_start=100,
serialized_end=176,
methods=[
_descriptor.MethodDescriptor(
name='SingleClassify',
full_name='classify.SingleClassify',
index=0,
containing_service=None,
input_type=_SINGLECLASSIFYREQUEST,
output_type=_SINGLECLASSIFYREPLY,
serialized_options=None,
),
])
_sym_db.RegisterServiceDescriptor(_CLASSIFY)
DESCRIPTOR.services_by_name['classify'] = _CLASSIFY
# @@protoc_insertion_point(module_scope)
# Generated by the gRPC Python protocol compiler plugin. DO NOT EDIT!
import grpc
import protobuf.classify_pb2 as classify__pb2
class classifyStub(object):
"""Missing associated documentation comment in .proto file"""
def __init__(self, channel):
"""Constructor.
Args:
channel: A grpc.Channel.
"""
self.SingleClassify = channel.unary_unary(
'/classify/SingleClassify',
request_serializer=classify__pb2.SingleClassifyRequest.SerializeToString,
response_deserializer=classify__pb2.SingleClassifyReply.FromString,
)
class classifyServicer(object):
"""Missing associated documentation comment in .proto file"""
def SingleClassify(self, request, context):
"""Missing associated documentation comment in .proto file"""
context.set_code(grpc.StatusCode.UNIMPLEMENTED)
context.set_details('Method not implemented!')
raise NotImplementedError('Method not implemented!')
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,
),
}
generic_handler = grpc.method_handlers_generic_handler(
'classify', rpc_method_handlers)
server.add_generic_rpc_handlers((generic_handler,))
# This class is part of an EXPERIMENTAL API.
class classify(object):
"""Missing associated documentation comment in .proto file"""
@staticmethod
def SingleClassify(request,
target,
options=(),
channel_credentials=None,
call_credentials=None,
compression=None,
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,
options, channel_credentials,
call_credentials, compression, wait_for_ready, timeout, metadata)
from concurrent import futures
import time
import grpc
from protobuf import classify_pb2
from protobuf import classify_pb2_grpc
class classify(classify_pb2_grpc.classifyServicer):
def SingleClassify(self, request, context):
return classify_pb2.SingleClassifyReply(message='result {msg}'.format(msg=request.keyword))
def serve():
# 启动 rpc 服务
server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
classify_pb2_grpc.add_classifyServicer_to_server(classify(), server)
server.add_insecure_port('[::]:50051')
server.start()
try:
while True:
time.sleep(60*60*24) # one day in seconds
except KeyboardInterrupt:
server.stop(0)
if __name__ == '__main__':
serve()
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