From 78b2bc6a9bab0eb6dc62b06011deee8f4b5e13d4 Mon Sep 17 00:00:00 2001 From: Mahmoud Hashemi Date: Wed, 4 Mar 2015 02:26:00 -0800 Subject: [PATCH] adding GCToggler/gcutils --- boltons/gcutils.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 boltons/gcutils.py diff --git a/boltons/gcutils.py b/boltons/gcutils.py new file mode 100644 index 0000000..807a645 --- /dev/null +++ b/boltons/gcutils.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- + +import gc + + +class GCToggler(object): + def __init__(self, postcollect=False): + self.postcollect = postcollect + + def __enter__(self): + gc.disable() + + def __exit__(self, exc_type, exc_val, exc_tb): + gc.enable() + if self.postcollect: + gc.collect() + + +toggle_gc = GCToggler() +toggle_gc_postcollect = GCToggler(postcollect=True)