python端Client跨线程调用predict报错(numpy输入)
Created by: barrierye
python端client跨线程用numpy预测的时候会偶发性报错(在不报错的情况下预测结果正常),list暂时没遇到这种情况。 可能是GIL的释放导致变量生命周期管理异常(list对应的predict的GIL部分不知道什么时候不见了)
报错信息:
# python profile_org_client.py
WARNING: Logging before InitGoogleLogging() is written to STDERR
I0522 18:25:04.466261 28517 naming_service_thread.cpp:209] brpc::policy::ListNamingService("127.0.0.1:9393"): added 1
Exception in thread Thread-3:
Traceback (most recent call last):
File "/usr/lib64/python2.7/threading.py", line 812, in __bootstrap_inner
self.run()
File "/usr/lib64/python2.7/threading.py", line 765, in run
self.__target(*self.__args, **self.__kwargs)
File "profile_org_client.py", line 37, in func
feed={"x": x}, fetch=["price"])
File "/home/barriery/.local/lib/python2.7/site-packages/paddle_serving_client/__init__.py", line 342, in predict
result_map[name].shape = shape
ValueError: cannot reshape array of size 0 into shape (1,1)
Exception in thread Thread-2:
Traceback (most recent call last):
File "/usr/lib64/python2.7/threading.py", line 812, in __bootstrap_inner
self.run()
File "/usr/lib64/python2.7/threading.py", line 765, in run
self.__target(*self.__args, **self.__kwargs)
File "profile_org_client.py", line 37, in func
feed={"x": x}, fetch=["price"])
File "/home/barriery/.local/lib/python2.7/site-packages/paddle_serving_client/__init__.py", line 342, in predict
result_map[name].shape = shape
ValueError: cannot reshape array of size 0 into shape (1,1)
复现代码:
from paddle_serving_client import Client
import numpy as np
import sys
import threading
import time
from line_profiler import LineProfiler
client = Client()
client.load_client_config("uci_housing_client/serving_client_conf.prototxt")
client.connect(["127.0.0.1:9393"])
import paddle
lx = [0.0137, -0.1136, 0.2553, -0.0692, 0.0582, -0.0727, -0.1583, -0.0584,
0.6283, 0.4919, 0.1856, 0.0795, -0.0332]
x = np.array(lx, dtype='float')
def func(num):
for i in range(num):
fetch_map = client.predict(
feed={"x": x}, fetch=["price"])
# print(fetch_map)
thread_num = 3
each_thread = 1000
ths = []
start = time.time()
for i in range(thread_num):
th = threading.Thread(target=func, args=(each_thread, ))
th.start()
ths.append(th)
for i in range(thread_num):
ths[i].join()
end = time.time()
print("time: {}".format(end - start))