Acquiring and Releasing Several Resources

When several resources have nested lifetimes, release them in an order consistent with their dependencies.

Source
Anbloq
Language
English
Created
Updated
Chapters
2
Reading time
1 min
In this article2 chapters
  1. Trace a report writer
  2. Exercise a partial acquisition

Trace a report writer

A stream writer may use an underlying file. Finish or close the writer before closing the file it needs for final output. Buffered writes show why closing in the wrong order can affect the result.

Exercise a partial acquisition

If the file opens and creating the writer fails, the file still needs cleanup. A context-managed lifetime can enclose construction of the dependent resource so that failure takes the established release path.[1]

References

  1. Python Software Foundation, Python Tutorial: Errors and Exceptions ↩