Managing resources using context manager
In this recipe, you will learn to update two progress bars simultaneously using two threads. Synchronizing between the two threads and locking them will be handled through the context manager. What is context manager? Let's have a quick look.
Context manager
Context manager enables us to allocate and release resources whenever desired. To optimize the use of resources, it is essential that, when any resources are allocated by any application or thread, they are freed or cleaned up so that they can be used by some other application or thread. But sometimes the program crashes while executing, or for some other reason the program does not terminate properly, and consequently, the allocated resources are not properly freed. Context managers help in such situations by ensuring the cleaning up of allocated resources takes place. Here is a small example of using the context manager:
with method_call() as variable_name:
statements that use variable_name
...