Python 3.2+sysfunction
sys.setswitchinterval()
interpreter
Sets the thread switch interval in seconds — how often the thread holding the GIL yields it to others. Rarely needed: for experiments and fine tuning.
Syntax
sys.setswitchinterval(interval)
A smaller interval makes I/O threads more responsive but adds switching overhead; a larger one does the opposite. It cannot speed up computation with threads. The value is process-wide, and zero or negative numbers raise ValueError. When you change it in tests, restore the old value or neighbouring tests will suffer.
Examples
import sys
old = sys.getswitchinterval()
sys.setswitchinterval(0.001)
print(sys.getswitchinterval()) # → 0.001
sys.setswitchinterval(old)
try:
sys.setswitchinterval(0)
except ValueError as e:
print(e) # → switch interval must be strictly positive
See also
Common errors
Open in the full glossarydocs.python.orgReport an inaccuracy