Message375557
The save-a-boolean-for-each-context-manager approach has an issue if used with concurrent generators, where the lifetimes of two generator objects might be overlapping but not completely nested, as shown below. The same issue should arise when using multiple threads. The approach in no_gc.py with counting the times_disabled does not run into the same issue.
>>> from contextlib import contextmanager
>>> import gc
>>> @contextmanager
def no_gc():
was_enabled = gc.isenabled()
try:
yield
finally:
if was_enabled:
gc.enable()
>>> def gen1():
with no_gc():
yield "a"
yield "b"
yield "c"
>>> def gen2():
with no_gc():
yield 1
yield 2
yield 3
>>>
>>> g1 = gen1()
>>> g2 = gen2()
>>> next(g1)
'a'
>>> next(g2)
1
>>> list(g1)
['b', 'c']
>>> gc.isenabled() # should be False!
True
>>> list(g2)
[2, 3] |
|
Date |
User |
Action |
Args |
2020-08-17 16:21:21 | Dennis Sweeney | set | recipients:
+ Dennis Sweeney, steven.daprano, ammar2, Yonatan Goldschmidt, iritkatriel |
2020-08-17 16:21:21 | Dennis Sweeney | set | messageid: <1597681281.46.0.568014999176.issue41545@roundup.psfhosted.org> |
2020-08-17 16:21:21 | Dennis Sweeney | link | issue41545 messages |
2020-08-17 16:21:21 | Dennis Sweeney | create | |
|