Context Manager in Python (original) (raw)

In any programming language, usage of resources like file operations or database connections is very common, but limited. Therefore, main problem lies in making sure to release these resources after usage. If they are not released then it will lead to resource leakage and may cause system to either slow down or crash.
Python’s **context managers provide a neat way to automatically set up and clean up resources, ensuring they’re properly managed even if errors occur.

Why Do We Need Context Managers?

Risks of Not Closing Resources

Failing to close resources like files can lead to serious issues, such as exhausting available system file descriptors. This can cause your program or even the entire system to slow down or crash.

**Example: This code repeatedly opens a file without closing it, leading to resource exhaustion and a potential system error.

Python `

file_descriptors = [] for x in range(100000): file_descriptors.append(open('test.txt', 'w'))

`

**This will raise:

Traceback (most recent call last):
File "context.py", line 3, in
OSError: [Errno 24] Too many open files: 'test.txt'

This happens because too many files are left open without being closed, something context managers help avoid.

Built-in Context Manager for File Handling

File operations are a common case in Python where proper resource management is crucial. The **with statement provides a built-in context manager that ensures file is automatically closed once you're done with it, even if an error occurs.

**Example: In this Example, we open a file using a context manager **with statement and reads its content safely.

Python `

with open("test.txt") as f: data = f.read()

`

This eliminates need to explicitly call **close() and protects against resource leakage in case of unexpected failures.

Creating Custom Context Manager Class

A class-based context manager needs two methods:

**Example:

The below example, shows how Python initializes the object, enters the context, runs the block and then exits while cleaning up.

Python `

class ContextManager: def init(self): print('init method called')

def __enter__(self):
    print('enter method called')
    return self

def __exit__(self, exc_type, exc_value, exc_traceback):
    print('exit method called')

with ContextManager() as manager: print('with statement block')

`

**Output:

init method called
enter method called
with statement block
exit method called

**Explanation:

File Management Using Context Manager

Applying the concept of custom context managers, this example defines a **FileManager class to handle opening, reading/writing and automatically closing a file.

**Example:

Python `

class FileManager: def init(self, filename, mode): self.filename = filename self.mode = mode self.file = None

def __enter__(self):
    self.file = open(self.filename, self.mode)
    return self.file

def __exit__(self, exc_type, exc_value, exc_traceback):
    self.file.close()

with FileManager('test.txt', 'w') as f: f.write('Test') print(f.closed)

`

**Output:

True

**Explanation:

Database Connection Management with Context Manager

Let's create a simple database connection management system. The number of database connections that can be opened at a time is also limited(just like file descriptors). Therefore context managers are helpful in managing connections to the database as there could be chances that programmer may forget to close the connection.

**Example:

Python `

from pymongo import MongoClient

class MongoDBConnectionManager: def init(self, hostname, port): self.hostname = hostname self.port = port self.connection = None

def __enter__(self):
    self.connection = MongoClient(self.hostname, self.port)
    return self.connection

def __exit__(self, exc_type, exc_value, exc_traceback):
    self.connection.close()

with MongoDBConnectionManager('localhost', 27017) as mongo: collection = mongo.SampleDb.test data = collection.find_one({'_id': 1}) print(data.get('name'))

`