Auto release custom func resource in Python

How to release a user-defined resource automatically?

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.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
import contextlib

@contextlib.contextmanager
def managed_custom_resource(*args, **kwargs):
    resource = acquire_resource(*args, **kwargs)
    try:
        yield resource
    finally:
        release_resource(resource)

def acquire_resource(*args, **kwargs):
    print('acquire_resource(*args={}, **kwargs={})'.format(args, kwargs))

    # Code to acquire resource, e.g.:
    resource = object()
    return resource

def release_resource(resource):
    print('release_resource(resource={})'.format(resource))

    # Code to release resource, e.g.:
    pass