From fdcdaebfd3c7319adc56c7cb35d12b976967efdb Mon Sep 17 00:00:00 2001 From: Monty Taylor Date: Mon, 29 Oct 2018 11:31:01 -0500 Subject: [PATCH] Make Connection a context manager For people who are wanting to make sure Connection resources are cleaned up, add __enter__ and __exit__ methods so that one can do: with Connection() as conn: conn.be_awesome() and be sure that things are cleaned properly. Change-Id: I077dd09de7859c8e81c8090c847ecd5d72c4318e --- openstack/connection.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/openstack/connection.py b/openstack/connection.py index d4968dc0f..73631d06e 100644 --- a/openstack/connection.py +++ b/openstack/connection.py @@ -371,3 +371,9 @@ class Connection(six.with_metaclass(_meta.ConnectionMeta, def close(self): """Release any resources held open.""" self.task_manager.stop() + + def __enter__(self): + return self + + def __exit__(self, exc_type, exc_value, traceback): + self.close()