I am using gRPC in a Python script, and most of my code is wrapped in this:
def main():
with grpc.insecure_channel(address) as channel:
...
I am trying to reduce the indentation depth of the code, and wondering if we can drop the with
. (One can argue if this is just vanity, or useful because it becomes more readable, but let's go with it for a second. I'm just curious about the behavior of gRPC here.) I would replace it with an explicit call to .close
, however this script is likely to be stopped with Ctrl+C most of the time so that wouldn't get called.
How bad is it if the channel doesn't get closed? My process is exiting anyway, so it won't leak memory. Will it clog a connection slot on the server? For how long? This script is going to be called rarely, manually, and not multiple instances at the same time.
question from:
https://stackoverflow.com/questions/65843011/do-i-need-to-call-channel-close-when-my-process-exits 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…