mirror of
https://github.com/python/cpython.git
synced 2025-08-31 05:58:33 +00:00
bpo-31356: Add context manager to temporarily disable GC (GH-4224)
This commit is contained in:
parent
0cd6bca655
commit
72a0d218dc
5 changed files with 208 additions and 1 deletions
|
@ -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.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue