diff --git a/memcache.py b/memcache.py index db8e632..d7b1f16 100644 --- a/memcache.py +++ b/memcache.py @@ -291,6 +291,11 @@ def get_stats(self, stat_args = None): return(data) + def quit_all(self): + '''Send a "quit" command to all servers and wait for the connection to close.''' + for s in self.servers: + s.quit() + def get_slabs(self): data = [] for s in self.servers: @@ -1215,6 +1220,23 @@ def recv(self, rlen): self.buffer = buf[rlen:] return buf[:rlen] + def quit(self): + '''Send a "quit" command to remote server and wait for connection to close.''' + if self.socket: + # Using self.send_cmd, so no need for '\r\n'. + self.send_cmd('quit') + + # We can't close the local socket until the remote end processes the quit + # command and sends us a FIN packet. When that happens, socket.recv() + # will stop blocking and return an empty string. If we try to close the + # socket before then, the OS will think we're initiating the connection + # close and will put the socket into TIME_WAIT. + self.socket.recv(1) + + # At this point, socket should be in CLOSE_WAIT. Closing the socket should + # release the port back to the OS. + self.close_socket() + def flush(self): self.send_cmd('flush_all') self.expect('OK')