Is there a easy way to shut down python grpc server gracefully?

10/16/2019

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?

-- Chakradar Raju
grpc
kubernetes
python

2 Answers

10/28/2019

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

-- Richard Belleville
Source: StackOverflow

10/16/2019

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')
-- Chakradar Raju
Source: StackOverflow