how to release a user-defined resource automatically
Problem
You want to release a resource returned by a custom function automatically when the resource is no longer needed.
Solution
According to the Python documentation, a context manager is an object that
defines the runtime context to be established when executing a with
statement. The context manager’s __enter__()
method is called when the with
statement is entered and the context manager’s __exit__()
method is called when the with
statement is exited. The __exit__()
method is passed
the exception type, value, and traceback. If the with
statement exits normally, the exception type, value, and traceback are None
.
Use the contextlib.contextmanager
decorator to create a context manager. The function must return a generator that yields exactly one value. The
value yielded is bound to the variable in the with
statement’s as
clause. The generator’s code block is executed when the with
statement is
entered and exited.
The following example shows how to use the contextlib.contextmanager
decorator to create a context manager from a function that returns a resource
object.
|
|