Skip to the content.

Reentrant Context Manager

Reentrant context managers can not only be used in multiple with statements, but may also be used inside a with statement that is already using the same context manager. Such as threading.RLockcontextlib.suppres()contextlib.redirect_stdout().

Here’s a very simple example of reentrant use:

>>> from contextlib import redirect_stdout
>>> from io import StringIO

>>> stream = StringIO()
>>> write_to_stream = redirect_stdout(stream)

>>> with write_to_stream:
...     print("This is written to the stream rather than stdout")
...     with write_to_stream:
...         print("This is also written to the stream")
...

>>> print("This is written directly to stdout")
This is written directly to stdout

>>> print(stream.getvalue())
This is written to the stream rather than stdout
This is also written to the stream

Note: Being reentrant is not the same thing as being thread safe.

References