Chakradar Raju
Chakradar Raju

Reputation: 2811

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

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?

Upvotes: 4

Views: 11413

Answers (2)

Richard Belleville
Richard Belleville

Reputation: 1620

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

Upvotes: 1

Chakradar Raju
Chakradar Raju

Reputation: 2811

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')

Upvotes: 11

Related Questions