Python 3.2 introduced a new GIL implementation by Antoine Pitrou which exposes the function sys.setswitchinterval
.
When would changing this be useful, and why?
One use will be to make sure that operation(s) run atomically, for example:
sw_interval = sys.getswitchinterval()
try:
# Setting the switch interval to a very big number to make sure that their will be no
# thread context switching while running the operations that came after.
sys.setswitchinterval(sys.maxint)
# Expressions run here will be atomic ....
finally:
sys.setswitchinterval(sw_interval)
Another use case will be to tune your code specially when you're facing the convoy effect (or any edge case where the new GIL give bad performance). Maybe (just maybe) changing the context switch interval can give you more speed.
Disclaimer: The first method cited above is considered dark magic and it's totally not recommended (the threading.Lock
-likes are preferred in that use case). In general I don't think that changing the thread context switch interval is something to do under normal circumstances. I will paraphrase what Tim Peters already say about metaclasses: changing thread context switch interval is deeper magic than 99% of people are going to need
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With