媒介
python具有丰富的看维并且很轻易作为胶水说话很轻易与c/c++进行交互集成。
在script目次下履行run_single.sh,即
是以为了适应快速变更的营业和兼顾枷⒚效力,在上层采取python作为server供给service,在底层采取c/c++进行计算是一种对于算法开辟者异常合适的方法。
python flask库供给http接口以及相干demo页面,gunicorn供给多核并行才能,底层c++库供给单线程上的计算。
下面经由过程一个例子解释这种架构。代码地址:python_hps
预备
在实验开端之前,须要安装flask、gunicorn、apach bench tool等对象。
注:所有实验均在linux体系中进行。测试机械为4核虚拟机。sudo pip install flask
sudo pip install gunicorn
sudo apt-get install apache2-utils
计算
计算部分模仿真实计算,是以计算量比较大年夜,在我测试的虚拟机上单核单线程跑400ms阁下。
c++核心计算部分,随便写的:API_DESCintfoo(constintval)
{
floatresult = 0.0f;
for(intc=0;c<1000;c++)
{
for(inti=0;i
{
result += (i);
result += sqrt((float)(i*i));
result += pow((float)(i*i*i),0.1f);
}
}
return(int)result;
}
python wrapper,采取ctypes:#python wrapperoflibfoo
class FooWrapper:
def __init__(self):
cur_path = os.path.abspath(os.path.dirname(__file__))
self.module = ctypes.CDLL(os.path.join(cur_path,'./impl/libfoo.so'))
def foo(self,val):
self.module.foo.argtypes = (ctypes.c_int,)
self.module.foo.restype = ctypes.c_int
result = self.module.foo(val)
returnresult
flask http API:@app.route('/api/foo',methods=['GET','POST'])
def handle_api_foo():
#get input
val = flask.request.json['val'
推荐阅读
电信行业肯定欲望在超高容量和低延迟的下一代收集的支撑下安排5G营业,然则在5G商用之前,该技巧须要做进一步>>>详细阅读
地址:http://www.17bianji.com/lsqh/35421.html