Can I get some context on context managers (Part 1)

Hello friends, lets put some context around context managers. Lets discuss why they are useful, what are they for, and how to use them. They are one of pythons useful features that can help keep you safe. But what are they really?

When dealing with resources we normally have a 3 phased approach that goes something like this;

  1. We open a connection handler to the resource
file = open('/nfs_mount/file_to_process.txt')

Great now we have our connection.

  1. Then we do something with that file.
for line in file.readlines():
  1. Then we close the file

That wasn't so bad was it. But what happens when say things go wrong? For instance the server crashes or the network card hiccups in the server or even in your machine running the python file. Even worse you forget to close the file afterwards. These can all lead to resource leaks, or unwanted errors.

Enter the context manager.

Here is where the contexct manager saves the day. The context manager helps with the management of resources and does so in a safe way. You commonly see this in the with statements. But how does this with statement work.

Well friends that is a great question. One that might be easier with some more code. Typically the above might be written to look like this

with open('/nfs_mount/file_to_process.txt') as infile:
    for line in infile.readlines():

Now you might be asking where is the file.close(). Well that is the magic part of this. The with statement automatically closes the resource after all the code under it executes. This is part of the resource management. Another way to think about the with statement might be to view it like this

    infile = open('/nfs_mount/file_to_process.txt')
    for line in infile.readlines():

As you can see, no matter what happens with the execution wether it be an exception error or completion, the file is always closed. So if while reading from the file or even writing and say something happens to the connection it is still close gracefully.

One point I want to make at this time is we have been dealing with files in all the examples, but you are not limited to just those. They could be network handles, or even handles to databases.

I hope this article was helpful to all. The takeaways are

  • Context managers help with resource management
  • They provide safe ways to connect to outside resources and manage those connections
  • They always close those connections even with errors which keep the resources from causing leaks

Next week I hope to extend on this and show you how you can add a context managers to your code.