通常软件开发过程中会出现语言异构现象,本文使用xmlrpc完成python和C++的调用,
主要参考https://blog.csdn.net/qq_28453017/article/details/79931620。
python客户端
from xmlrpc.client import ServerProxy
import time
tic = time.time()
s = ServerProxy("http://localhost:8088")
result = s.add(1,2)
toc = time.time()
print(result ,toc-tic)
若将localhost 改为127.0.0.1
可以将速度提升至每秒88次
C++服务端
配置
主要参考https://blog.csdn.net/qq_28453017/article/details/79931620。
具体细节过程中:
- 运行ConfigureWin32.bat文件
- 新建C++工程
- Additional Include Directories 引入xmlrpc-c-code-r3067-super_stable\include
- Library Directories 引入 D:\task\temp_task\xmlrpc\xmlrpc-c-code-r3067-super_stable\bin\Debug-Static-Win32
- Link->input->Additional Library 引入
libxmlrpc++.lib
libxmlrpc.lib
libxmlrpc_abyss++.lib
libxmlrpc_abyss.lib
libxmlrpc_client.lib
libxmlrpc_server.lib
libxmlrpc_server_abyss.lib
libxmlrpc_server_cgi.lib
libxmlrpc_server_w32httpsys.lib
libxmlrpc_util++.lib
libxmlrpc_util.lib
libxmlrpc_xmlparse.lib
libxmlrpc_xmltok.lib
代码
#include <assert.h>
#include <iostream>
#define XMLRPC_CPP 1
#define XMLRPC_C 0
#if XMLRPC_CPP
#include <xmlrpc-c/base.hpp>
#include <xmlrpc-c/registry.hpp>
#include <xmlrpc-c/server_abyss.hpp>
class sampleAddMethod : public xmlrpc_c::method {
public:
sampleAddMethod(){}
void execute(xmlrpc_c::paramList const& paramList,
xmlrpc_c::value* const retvalP =NULL) {
int a = paramList.getInt(0);
int b = paramList.getInt(1);
*retvalP = xmlrpc_c::value_int(a+b);
/*std::cout << "struct success**" << std::endl;
assert(struct_param["first"].type() == xmlrpc_c::value::TYPE_INT);
assert(struct_param["first"].type() == xmlrpc_c::value::TYPE_INT);
xmlrpc_c::value_int value1 = struct_param["first"];
xmlrpc_c::value_int value2 = struct_param["second"];
int const addend = value1.cvalue();
int const adder = value2.cvalue();
paramList.verifyEnd(1);
*retvalP = xmlrpc_c::value_int(addend + adder);*/
}
};
#endif
int main()
{
//std::cout << "Hello World!\n";
xmlrpc_c::registry myRegistry;
xmlrpc_c::methodPtr const sampleAddMethodP(new sampleAddMethod);
myRegistry.addMethod("add", sampleAddMethodP);
xmlrpc_c::serverAbyss myAbyssServer(myRegistry, 8088 // TCP port on which to listen
//"/tmp/xmlrpc_log" // Log file
);
myAbyssServer.run(); // xmlrpc_c::serverAbyss.run() never returns
assert(false);
return 0;
}