简体   繁体   English

对C ++对象的并发远程过程调用

[英]Concurrent Remote Procedure Calls to C++ Object

I would like to have a class written in C++ that acts as a remote procedure call server. 我想要一个用C ++编写的类,它充当远程过程调用服务器。

I have a large (over a gigabyte) file that I parse, reading in parameters to instantiate objects which I then store in a std::map. 我解析了一个大文件(超过1 GB),读取了参数以实例化对象,然后将其存储在std :: map中。 I would like the RPC server to listen for calls from a client, take the parameters passed from the client, look up an appropriate value in the map, do a calculation, and return the calculated value back to the client, and I want it to serve concurrent requests -- so I'd like to have multiple threads listening. 我希望RPC服务器侦听来自客户端的调用,获取从客户端传递的参数,在映射中查找适当的值,进行计算,然后将计算出的值返回给客户端,我希望它服务并发请求-所以我想让多个线程在监听。 BTW, after the map is populated, it does not change. 顺便说一句,在填充地图后,它不会更改。 The requests will only read from it. 请求将仅从中读取。

I'd like to write the client in Python. 我想用Python编写客户端。 Could the server just be an HTTP server that listens for POST requests, and the client can use urllib to send them? 该服务器可以只是侦听POST请求的HTTP服务器,而客户端可以使用urllib发送它们吗?

I'm new to C++ so I have no idea how to write the server. 我是C ++的新手,所以我不知道如何编写服务器。 Can anyone point me to some examples? 谁能给我指出一些例子?

Maybe factors can affect the selection. 也许因素会影响选择。

One solution is to use fastcgi. 一种解决方案是使用fastcgi。

Client sends HTTP request to HTTP server that has fastCGI enabled. 客户端将HTTP请求发送到已启用fastCGI的HTTP服务器。 HTP server dispatch the request to your RPC server via the fastcgi mechanism. HTP服务器通过fastcgi机制将请求调度到您的RP​​C服务器。 RPC server process and generates response, and sends back to http server. RPC服务器处理并生成响应,并将其发送回http服务器。 http server sends the response back to your client. http服务器将响应发送回您的客户端。

If you must write the server in C++, and you have no idea how to do so, you definitely want to use a framework. 如果必须使用C ++编写服务器,而又不知道如何编写服务器,则肯定要使用框架。

There are many choices out there, and StackOverflow is not a good place to ask for opinions about different choices. 那里有很多选择,而StackOverflow并不是寻求关于不同选择的意见的好地方。 But to give you one example, JsonRpc-Cpp lets you implement a JSON-RPC (over either raw TCP or HTTP) server in about 10 lines of code, plus a line of boilerplate for each RPC you want to expose. 但是,举一个例子, JsonRpc-Cpp允许您以大约10行代码来实现JSON-RPC(通过原始TCP或HTTP)服务器,并为要公开的每个RPC添加一行样板。 Much easier than writing a server from scratch. 这比从头开始编写服务器要容易得多。

So, here's an example server: 因此,这是一个示例服务器:

#include <cstdlib>
#include <jsonrpc.h>

class Service {
public:
    bool echo(const Json::Value &root, Json::Value &response) {
        response["jsonrpc"] = "2.0";
        response["id"] = root["id"];
        Json::Value result;
        result["params"] = root["params"];
        response["result"] = result;
        return response;
    }
}

int main(int argc, char *argv[]) {
    int port = std::atoi(argv[1]); // add some error handling here...

    Json::Rpc::TcpServer server("127.0.0.1", port);
    Service service;
    server.AddMethod(new Json::Rpc::RpcMethod<Service>(service,
                     &Service::echo, "echo");
    server.Bind(); // add error handling again
    server.Listen();
    while(true) server.WaitMessage(1000);
}

However, it will probably be even easier if you write the server in Python, or use a WSGI web server and write a service in Python, and call the C++ code from Python (whether via ctypes , or by building a wrapper with something like Cython or SIP). 但是,如果您使用Python编写服务器,或使用WSGI Web服务器并使用Python编写服务,然后从Python调用C ++代码(无论是通过ctypes还是通过使用Cython构建包装器),则可能会更加容易。或SIP)。

Here's the same server in Python using bjsonrpc : 这是使用bjsonrpc Python中的同一服务器:

import bjsonrpc

class Service(bjsonrpc.handlers.BaseHandler):
    def echo(self, *args):
        return {'params': args}

server = bjsonrpc.createserver(handler_factory = Service)
server.serve()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM