Python : Custom Context Managers
Operating System resources must be released after our use. Some common resource handles are file, sockets, thread locks etc. So if our program is using these resources we need to ensure that we are releasing it after our use. A general pattern of handling resource is like this.
def read_file():
f = open(filename, 'r')
data = f.read()
f.close()
What will happen if there was an exception in between and f.close() never happens ? That means we are not properly cleaning the resource handle. Context managers helps us to do this in a clean way as shown below.
def read_file():
with open(filename, 'r') as f:
data = f.read()
f.close()
In this case its guaranteed that the file handle gets released. Let us see how context managers works.
You can handle context support to your type, by implementing two special methods as shown below.
class Context(object):
def __enter__(self):
print('Entering..')
return 'Some value'
def __exit__(self,ty, val, tb):
print('Exiting..')
print(ty, val, tb)
# sample
from context import Context
c = Context()
with c:
print('some work')
output :
# outputs
Entering..
some work
Exiting..
None None None
As you can see Entering and Exit functions gets called and we can do our resource handling here.
Coding is fun enjoy…