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
This commit is contained in:
Monty Taylor 2018-10-29 11:31:01 -05:00
parent ac1935b182
commit fdcdaebfd3
No known key found for this signature in database
GPG Key ID: 7BAE94BC7141A594
1 changed files with 6 additions and 0 deletions

View File

@ -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()