summaryrefslogtreecommitdiffstats
path: root/Doc/library/gc.rst
diff options
context:
space:
mode:
authorPablo Galindo <Pablogsal@gmail.com>2018-01-29 20:37:09 (GMT)
committerRaymond Hettinger <rhettinger@users.noreply.github.com>2018-01-29 20:37:09 (GMT)
commit72a0d218dcc94a3cc409a9ef32dfcd5a7bbcb43c (patch)
treedce812414bdc6029522a92a6afd10dd00fdc8f06 /Doc/library/gc.rst
parent0cd6bca65519109a8a7862d38ba1b8924e432a16 (diff)
downloadcpython-72a0d218dcc94a3cc409a9ef32dfcd5a7bbcb43c.zip
cpython-72a0d218dcc94a3cc409a9ef32dfcd5a7bbcb43c.tar.gz
cpython-72a0d218dcc94a3cc409a9ef32dfcd5a7bbcb43c.tar.bz2
bpo-31356: Add context manager to temporarily disable GC (GH-4224)
Diffstat (limited to 'Doc/library/gc.rst')
-rw-r--r--Doc/library/gc.rst28
1 files changed, 28 insertions, 0 deletions
diff --git a/Doc/library/gc.rst b/Doc/library/gc.rst
index 153d8fb..92240c7 100644
--- a/Doc/library/gc.rst
+++ b/Doc/library/gc.rst
@@ -33,6 +33,34 @@ The :mod:`gc` module provides the following functions:
Disable automatic garbage collection.
+.. class:: ensure_disabled()
+
+ Return a context manager object that disables the garbage collector and reenables the previous
+ state upon completion of the block. This is basically equivalent to::
+
+ from gc import enable, disable, isenabled
+
+ @contextmanager
+ def ensure_disabled():
+ was_enabled_previously = isenabled()
+ gc.disable()
+ yield
+ if was_enabled_previously:
+ gc.enable()
+
+ And lets you write code like this::
+
+ with ensure_disabled():
+ run_some_timing()
+
+ with ensure_disabled():
+ # do_something_that_has_real_time_guarantees
+ # such as a pair trade, robotic braking, etc
+
+ without needing to explicitly enable and disable the garbage collector yourself.
+ This context manager is implemented in C to assure atomicity, thread safety and speed.
+
+
.. function:: isenabled()
Returns true if automatic collection is enabled.