##// END OF EJS Templates
nocg: make the utility work are both a decorator and context manager...
marmoute -
r52442:a452807d default
parent child Browse files
Show More
@@ -35,6 +35,7 b' import traceback'
35 import warnings
35 import warnings
36
36
37 from typing import (
37 from typing import (
38 Any,
38 Iterable,
39 Iterable,
39 Iterator,
40 Iterator,
40 List,
41 List,
@@ -1812,7 +1813,7 b' def never(fn):'
1812 return False
1813 return False
1813
1814
1814
1815
1815 def nogc(func):
1816 def nogc(func=None) -> Any:
1816 """disable garbage collector
1817 """disable garbage collector
1817
1818
1818 Python's garbage collector triggers a GC each time a certain number of
1819 Python's garbage collector triggers a GC each time a certain number of
@@ -1825,15 +1826,27 b' def nogc(func):'
1825 This garbage collector issue have been fixed in 2.7. But it still affect
1826 This garbage collector issue have been fixed in 2.7. But it still affect
1826 CPython's performance.
1827 CPython's performance.
1827 """
1828 """
1828
1829 if func is None:
1830 return _nogc_context()
1831 else:
1832 return _nogc_decorator(func)
1833
1834
1835 @contextlib.contextmanager
1836 def _nogc_context():
1837 gcenabled = gc.isenabled()
1838 gc.disable()
1839 try:
1840 yield
1841 finally:
1842 if gcenabled:
1843 gc.enable()
1844
1845
1846 def _nogc_decorator(func):
1829 def wrapper(*args, **kwargs):
1847 def wrapper(*args, **kwargs):
1830 gcenabled = gc.isenabled()
1848 with _nogc_context():
1831 gc.disable()
1832 try:
1833 return func(*args, **kwargs)
1849 return func(*args, **kwargs)
1834 finally:
1835 if gcenabled:
1836 gc.enable()
1837
1850
1838 return wrapper
1851 return wrapper
1839
1852
General Comments 0
You need to be logged in to leave comments. Login now