I try to realize the python call c++ by grpc, in client use the python and sever is c++, c++ is correct, but when I run ‘python3 client.py’ is raise error details = “failed to connect to all addresses; last error: UNKNOWN: ipv4:127.0.0.1:50004: Failed to connect to remote host: connect: Connection refused (111)”.
Anyone could help me? Thanks a lot!!!!!!
Code is from web.
proto:
syntax = "proto3";
package comm;
service ImageService {
rpc get_batch (BatchRequest) returns (BatchReply) {}
}
message BatchRequest {
}
message BatchReply {
bytes data = 1;
repeated int32 shape = 2;
string dtype = 3;
repeated int64 labels = 4;
}
c++:
//server.cpp
#include <iostream>
#include <string>
#include <vector>
#include <memory>
#include <chrono>
#include <grpcpp/grpcpp.h>
#include "interface.grpc.pb.h"
#include "interface.pb.h"
using std::string;
using std::vector;
using std::unique_ptr;
using std::cout;
using std::endl;
using grpc::Status;
using grpc::ServerBuilder;
using grpc::ServerContext;
using grpc::ServerWriter;
class ImageServiceImpl final: public comm::ImageService::Service {
public:
ImageServiceImpl();
Status get_batch(ServerContext *context, const comm::BatchRequest *request, comm::BatchReply* reply) override;
};
ImageServiceImpl::ImageServiceImpl() {}
Status ImageServiceImpl::get_batch(ServerContext *context,
const comm::BatchRequest *request,
comm::BatchReply* reply) {
int64_t buf_len = 1024 * 224 * 224 * 3 * sizeof(float);
vector<float> imbuf(buf_len);
auto start = std::chrono::steady_clock::now();
for (int i{0}; i < 1024; ++i) reply->add_labels(1);
reply->add_shape(1024);
reply->add_shape(224);
reply->add_shape(224);
reply->add_shape(3);
reply->set_dtype("float32");
string *sptr = reply->mutable_data();
sptr->resize(buf_len);
memset(&(sptr->at(0)), 1, buf_len);
auto end = std::chrono::steady_clock::now();
cout << "time is: " << std::chrono::duration<double, std::milli>(end - start).count() << endl;
return Status::OK;
}
int main() {
string addr("0.0.0.0:50004");
ImageServiceImpl service;
ServerBuilder builder;
builder.AddListeningPort(addr, grpc::InsecureServerCredentials());
builder.RegisterService(&service);
builder.SetMaxSendMessageSize(1L << 31);
builder.SetMaxReceiveMessageSize(1L << 31);
std::unique_ptr<grpc::Server> server(builder.BuildAndStart());
std::cout << "service started, listening to: " << addr << std::endl;
server->Wait();
return 0;
}
python:
import logging
import time
import sys
sys.path.insert(0, './commpy')
import grpc
import numpy as np
from interface_pb2 import BatchRequest
from interface_pb2_grpc import ImageServiceStub
def run_get_batch():
with grpc.insecure_channel(
'localhost:50004',
options=[
('grpc.max_receive_message_length', 1 << 30),
('grpc.max_send_message_length', 1 << 30),
]
) as channel:
stub = ImageServiceStub(channel)
req = BatchRequest()
reply = stub.get_batch(req)
ims = np.frombuffer(
reply.data, dtype=reply.dtype).reshape(reply.shape)
print(ims[:4, :1, :1, 0].ravel())
if __name__ == "__main__":
logging.basicConfig()
run_get_batch()
1