I want to have an object conManager
that is a reentrant context manager instance such that whenever I enter and exit its context it will print a number, but the number must be one higher than the number of the previus context (starting at 0).
Example:
with conManager:
print("Afirst")
with conManager:
print("Asecond")
with conManager:
print("third")
print("Bsecond")
print("Bfirst")
Output expected:
0
Afirst
1
Asecond
2
third
2
Bsecond
1
Bfirst
0
The only solution I have so far is a class with a stack in it, but that's not concurrent-safe. Are there any concurrent-safe solutions?
EDIT: as Sraw pointed out, I said thread safe when I meant concurrent-safe, changed the question accordingly.