7.12 Managing multiple contexts with multiple resources
We often use context managers with open files. Because the context manager can guarantee the OS resources are released, doing so prevents resource leaks. It can be used to prevent files from being closed without having all buffers flushed to persistent storage.
When multiple resources are being processed, it often means multiple context managers will be needed. For example, if we have three open files, we could require three nested with statements? How can we optimize or simplify multiple with statements?
7.12.1 Getting ready
We’ll look at creating a plan for a journey with multiple legs. Our starting data collection is a list of points that define our route. For example, traveling through Chesapeake Bay may involve starting in Annapolis, Maryland, sailing to Solomon’s Island, Deltaville, Virginia, and then Norfolk, Virginia. For planning purposes, we’d like to...