博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
试验thrift做后端rpc,nginx做web服务器, python后端php前端
阅读量:7201 次
发布时间:2019-06-29

本文共 2033 字,大约阅读时间需要 6 分钟。

因为后端的服务很负责,训练的模型都是基于python的tensorflow的,所以用RPC(remote procedual comminication);

接口用的是php写的,方便http协议调用;

过程就是http:www.../... -> nginx-> POST -> 参数传到test.php,调用  -> 通过RPC -> server.py处理  

server.py处理之后,通过RPC返回到test.php再嵌入到html文档里面,这样一个post+RPC的过程就完成了;

php+python的模式是线上服务使用的,我这里实验了一下,成功跑通了,分享几个最基本的文件;

example.thrift:

namespace py example namespace php example service Alice {    string ask(1:string question)}

然后用thrift --gen php example.thrift 

thrift --gen py example.thrift

下面的是客户端的test.php:

registerNamespace('Thrift', '/home/yanjianfeng/nginx/thrift/thrift-0.10.0/lib/php/lib');$loader->registerDefinition('example', $GEN_DIR);$loader->register();use Thrift\Protocol\TBinaryProtocol;use Thrift\Transport\TSocket;use Thrift\Transport\THttpClient;use Thrift\Transport\TBufferedTransport;use Thrift\Exception\TException;try { $socket = new TSocket('localhost', 30303); $transport = new TBufferedTransport($socket, 1024, 1024); $protocol = new TBinaryProtocol($transport); $client = new \example\AliceClient($protocol); $transport->open(); $ques = $_POST["ques"]; $sum = $client->ask($ques); echo $sum;} catch (TException $tx) { print 'TException: '.$tx->getMessage()."\n";}?>

最后是我用的服务器程序,很简单的一个返回:

#encoding=utf-8import syssys.path.append('./gen-py')from example import Alice from example.ttypes import *from thrift.transport import TSocketfrom thrift.transport import TTransportfrom thrift.protocol import TBinaryProtocolfrom thrift.server import TServerclass chat:    def __init__(self):        print 'initializat'    def ask(self, ques):        return ques + '猪\n' handler = chat()processor = Alice.Processor(handler)transport = TSocket.TServerSocket('localhost',30303)tfactory = TTransport.TBufferedTransportFactory()pfactory = TBinaryProtocol.TBinaryProtocolFactory()server = TServer.TSimpleServer(processor, transport, tfactory, pfactory)print "Starting python server..."server.serve()print "done!"
DEMO
ques

 

转载于:https://www.cnblogs.com/LarryGates/p/6596232.html

你可能感兴趣的文章
如何对第一个Vue.js组件进行单元测试 (下)
查看>>
关于UGUI中Canvas无法移动的解决方法
查看>>
Linux strace检查程序的系统调用及时间
查看>>
一个立体感的按钮样式
查看>>
下载安装MariaDB Galera 10.1
查看>>
传美团支付无证经营被央行叫停 限期三个月整改
查看>>
Redis笔记3-redis事务
查看>>
日本保险公司引进IBM Watson,裁员近30%
查看>>
第64天:CSS常用命名规范,有用!
查看>>
PHP代码审计笔记--命令执行漏洞
查看>>
redis成长之路——(七)
查看>>
[转]JDK、JRE、JVM三者间的关系
查看>>
TomTom收购Autonomos,自动驾驶又有新玩家入场
查看>>
DOM(文档对象模型)基础加强
查看>>
Ubuntu 16.04下安装Charles抓包工具
查看>>
python常用数据类型操作-时间日历
查看>>
Expert 诊断优化系列------------------你的CPU高么?
查看>>
量子电路制冷器:让量子计算更加可靠!
查看>>
大数据项目为什么失败,2017年将有何不同
查看>>
区块链概念1:Hash 算法
查看>>