Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Serving
提交
59d9ad8c
S
Serving
项目概览
PaddlePaddle
/
Serving
大约 1 年 前同步成功
通知
185
Star
833
Fork
253
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
105
列表
看板
标记
里程碑
合并请求
10
Wiki
2
Wiki
分析
仓库
DevOps
项目成员
Pages
S
Serving
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
105
Issue
105
列表
看板
标记
里程碑
合并请求
10
合并请求
10
Pages
分析
分析
仓库分析
DevOps
Wiki
2
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
59d9ad8c
编写于
8月 13, 2021
作者:
H
HexToString
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
update http_session
上级
3be11a3a
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
48 addition
and
22 deletion
+48
-22
python/paddle_serving_client/httpclient.py
python/paddle_serving_client/httpclient.py
+48
-22
未找到文件。
python/paddle_serving_client/httpclient.py
浏览文件 @
59d9ad8c
...
...
@@ -84,7 +84,7 @@ class GeneralClient(object):
self
.
feed_shapes_
=
{}
self
.
feed_types_
=
{}
self
.
feed_names_to_idx_
=
{}
self
.
timeout_
ms
=
20000
0
self
.
timeout_
s
=
2
0
self
.
ip
=
ip
self
.
port
=
port
self
.
server_port
=
port
...
...
@@ -96,6 +96,17 @@ class GeneralClient(object):
self
.
http_proto
=
True
self
.
max_body_size
=
512
*
1024
*
1024
self
.
use_grpc_client
=
False
# 使用连接池能够不用反复建立连接
self
.
requests_session
=
requests
.
session
()
# 初始化grpc_stub
options
=
[(
'grpc.max_receive_message_length'
,
self
.
max_body_size
),
(
'grpc.max_send_message_length'
,
self
.
max_body_size
)]
endpoints
=
[
self
.
ip
+
":"
+
self
.
server_port
]
g_endpoint
=
'ipv4:{}'
.
format
(
','
.
join
(
endpoints
))
self
.
channel_
=
grpc
.
insecure_channel
(
g_endpoint
,
options
=
options
)
self
.
stub_
=
general_model_service_pb2_grpc
.
GeneralModelServiceStub
(
self
.
channel_
)
def
load_client_config
(
self
,
model_config_path_list
):
if
isinstance
(
model_config_path_list
,
str
):
...
...
@@ -155,21 +166,24 @@ class GeneralClient(object):
def
set_max_body_size
(
self
,
max_body_size
):
self
.
max_body_size
=
max_body_size
self
.
init_grpc_stub
()
def
set_timeout_
ms
(
self
,
timeout_m
s
):
if
not
isinstance
(
timeout_
m
s
,
int
):
raise
ValueError
(
"timeout_
m
s must be int type."
)
def
set_timeout_
s
(
self
,
timeout_
s
):
if
not
isinstance
(
timeout_s
,
int
):
raise
ValueError
(
"timeout_s must be int type."
)
else
:
self
.
timeout_
ms
=
timeout_m
s
self
.
timeout_
s
=
timeout_
s
def
set_ip
(
self
,
ip
):
self
.
ip
=
ip
self
.
init_grpc_stub
()
def
set_service_name
(
self
,
service_name
):
self
.
service_name
=
service_name
def
set_port
(
self
,
port
):
self
.
port
=
port
self
.
init_grpc_stub
()
def
set_request_compress
(
self
,
try_request_gzip
):
self
.
try_request_gzip
=
try_request_gzip
...
...
@@ -195,13 +209,13 @@ class GeneralClient(object):
req
=
json
.
dumps
({
"key"
:
base64
.
b64encode
(
self
.
key
).
decode
()})
else
:
req
=
json
.
dumps
({})
r
=
requests
.
post
(
encrypt_url
,
req
)
result
=
r
.
json
()
if
"endpoint_list"
not
in
result
:
raise
ValueError
(
"server not ready"
)
else
:
self
.
server_port
=
str
(
result
[
"endpoint_list"
][
0
])
print
(
"rpc port is "
,
self
.
server_port
)
with
requests
.
post
(
encrypt_url
,
data
=
req
,
timeout
=
self
.
timeout_s
)
as
r
:
result
=
r
.
json
()
if
"endpoint_list"
not
in
result
:
raise
ValueError
(
"server not ready"
)
else
:
self
.
server_port
=
str
(
result
[
"endpoint_list"
][
0
])
print
(
"rpc port is "
,
self
.
server_port
)
def
get_feed_names
(
self
):
return
self
.
feed_names_
...
...
@@ -444,7 +458,10 @@ class GeneralClient(object):
try
:
if
self
.
try_request_gzip
and
self
.
total_data_number
>
512
:
origin_data
=
postData
postData
=
gzip
.
compress
(
bytes
(
postData
,
'utf-8'
))
if
http_proto
:
postData
=
gzip
.
compress
(
postData
)
else
:
postData
=
gzip
.
compress
(
bytes
(
postData
,
'utf-8'
))
headers
[
"Content-Encoding"
]
=
"gzip"
if
self
.
try_response_gzip
:
headers
[
"Accept-encoding"
]
=
"gzip"
...
...
@@ -453,10 +470,13 @@ class GeneralClient(object):
print
(
"compress error, we will use the no-compress data"
)
headers
.
pop
(
"Content-Encoding"
,
"nokey"
)
postData
=
origin_data
# requests支持自动识别解压
try
:
result
=
requests
.
post
(
url
=
web_url
,
headers
=
headers
,
data
=
postData
)
result
=
self
.
requests_session
.
post
(
url
=
web_url
,
headers
=
headers
,
data
=
postData
,
timeout
=
self
.
timeout_s
)
except
:
print
(
"http post error"
)
return
None
...
...
@@ -484,6 +504,15 @@ class GeneralClient(object):
postData
=
self
.
process_proto_data
(
feed_dict
,
fetch_list
,
batch
,
log_id
)
try
:
resp
=
self
.
stub_
.
inference
(
postData
,
timeout
=
self
.
timeout_s
)
except
:
print
(
"Grpc inference error occur"
)
return
None
else
:
return
resp
def
init_grpc_stub
(
self
):
# https://github.com/tensorflow/serving/issues/1382
options
=
[(
'grpc.max_receive_message_length'
,
self
.
max_body_size
),
(
'grpc.max_send_message_length'
,
self
.
max_body_size
)]
...
...
@@ -493,10 +522,7 @@ class GeneralClient(object):
self
.
channel_
=
grpc
.
insecure_channel
(
g_endpoint
,
options
=
options
)
self
.
stub_
=
general_model_service_pb2_grpc
.
GeneralModelServiceStub
(
self
.
channel_
)
try
:
resp
=
self
.
stub_
.
inference
(
postData
,
timeout
=
self
.
timeout_ms
)
except
:
print
(
"Grpc inference error occur"
)
return
None
else
:
return
resp
def
__del__
(
self
):
self
.
requests_session
.
close
()
self
.
channel_
.
close
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录