【发布时间】:2022-02-02 16:35:06
【问题描述】:
wait() 函数的 grpc 文档指出: ...或其他一些线程必须调用 Shutdown 才能使此函数返回。 我不得不承认,我不知道该怎么做。有什么帮助吗?谢谢。
【问题讨论】:
wait() 函数的 grpc 文档指出: ...或其他一些线程必须调用 Shutdown 才能使此函数返回。 我不得不承认,我不知道该怎么做。有什么帮助吗?谢谢。
【问题讨论】:
Shutdown() 是服务器接口上的一个方法: https://grpc.github.io/grpc/cpp/classgrpc_1_1_server_interface.html#a6a1d337270116c95f387e0abf01f6c6c
另一个线程需要在您定义的某个触发器上调用它以彻底关闭 gRPC 服务器。
【讨论】:
我想出了以下解决方案(基于 Hello World 示例)。任何 cmet 表示赞赏。
/*
*
* Copyright 2015 gRPC authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
///The trace macro
#if defined NDEBUG
#define TRACE( format, ... )
#else
#define TRACE( format, ... ) printf( "+++ %s::%s(%d) +++ " format, __FILE__, __FUNCTION__, __LINE__, __VA_ARGS__ )
#endif
#include <iostream>
#include <memory>
#include <string>
//Add
#include <pthread.h>
#include <grpcpp/ext/proto_server_reflection_plugin.h>
#include <grpcpp/grpcpp.h>
#include <grpcpp/health_check_service_interface.h>
#ifdef BAZEL_BUILD
#include "examples/protos/helloworld.grpc.pb.h"
#else
#include "helloworld.grpc.pb.h"
#endif
using grpc::Server;
using grpc::ServerBuilder;
using grpc::ServerContext;
using grpc::Status;
using helloworld::Greeter;
using helloworld::HelloReply;
using helloworld::HelloRequest;
// Logic and data behind the server's behavior.
class GreeterServiceImpl final : public Greeter::Service {
//Add constructor and destructor (optional)
GreeterServiceImpl() :
Greeter::Service () {
TRACE("Executing GreeterServiceImpl constructor%s", "\n");
}
~GreeterServiceImpl() {
TRACE("Executing GreeterServiceImpl destructor%s", "\n");
}
Status SayHello(ServerContext* context, const HelloRequest* request,
HelloReply* reply) override {
std::string prefix("Hello ");
reply->set_message(prefix + request->name());
return Status::OK;
}
};
pthread_t tid; // Shutdown thread handle
//thread function
void* doShutdown(void *server)
{
TRACE("Entering doShutdown %s", "\n");
getchar(); // press a key to shutdown the thread
((Server*)(server))->Shutdown();
std::cout << "Server is shutting down. "<< std::endl;
return NULL;
}
void RunServer() {
std::string server_address("0.0.0.0:50051");
GreeterServiceImpl service;
grpc::EnableDefaultHealthCheckService(true);
grpc::reflection::InitProtoReflectionServerBuilderPlugin();
ServerBuilder builder;
// Listen on the given address without any authentication mechanism.
builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
// Register "service" as the instance through which we'll communicate with
// clients. In this case it corresponds to an *synchronous* service.
builder.RegisterService(&service);
// Finally assemble the server.
std::unique_ptr<Server> server(builder.BuildAndStart());
std::cout << "Server listening on " << server_address << std::endl;
// Wait for the server to shutdown. Note that some other thread must be
// responsible for shutting down the server for this call to ever return.
int err = pthread_create(&(tid), NULL, &doShutdown, &(*server));
if (err != 0)
TRACE("Can't create Shutdown thread :[%s]", strerror(err));
else
std::cout << "Press the <ENTER> key to shutdown the server." << std::endl;
server->Wait();
}
int main(int argc, char** argv) {
RunServer();
return 0;
}
【讨论】: