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:
parent
ac1935b182
commit
fdcdaebfd3
|
@ -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()
|
||||
|
|
Loading…
Reference in New Issue