4

Here is a blog explaining how to gracefully shutdown a GRPC server in kotlin.

Is this the only way to do it? Counting live calls and handling SIGTERM manually? This should have been normal behavior.

I couldn't find how to count live calls in python. Can someone point me to docs that will help?

2 Answers 2

10

Turns out there is a easy way instead of counting RPCs, here is how I got it done:

server = grpc.server(futures.ThreadPoolExecutor(max_workers=100))
{} = {}Impl()
add_{}Servicer_to_server({}, server)
server.add_insecure_port('[::]:' + port)
server.start()
logger.info('Started server at ' + port)
done = threading.Event()
def on_done(signum, frame):
    logger.info('Got signal {}, {}'.format(signum, frame))
    done.set()
signal.signal(signal.SIGTERM, on_done)
done.wait()
logger.info('Stopped RPC server, Waiting for RPCs to complete...')
server.stop(NUM_SECS_TO_WAIT).wait()
logger.info('Done stopping server')
0
1

gRPC Python servers have a (newish) method for this. Just call server.wait_for_termination()

2
  • Does that handle SIGTERM? Also see my answer Oct 29, 2019 at 15:16
  • 1
    It doesn't handle SIGTERM Jul 21, 2020 at 17:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.