How often does Python switch threads?

In cPython (the default Python implementation), how often does thread switching happen? It is not a matter of using multiple cores; I know about GIL and understand that at the moment only one thread will be started.

I have seen several cases where I have two log messages in sequence, and the first log message will be emitted, but the second will not be emitted in a few seconds. This is probably because Python decided to switch threads between the two log statements. This makes me think that Python switches between threads once every couple of seconds, which is much slower than I expected. Is it correct?

+2
source share
1 answer

By default, Python 2 will switch threads every 100 commands. This can be configured using the sys.setcheckintervalone described here: https://docs.python.org/2/library/sys.html#sys.setcheckinterval

I found more information on pages 10, 11 and 12 of this presentation: http://www.dabeaz.com/python/UnderstandingGIL.pdf

(These answers come from comments on this subject, but since no one wrote them in response, I will do it myself. More than 6 months have passed since the comments were written.)

+5
source

Source: https://habr.com/ru/post/1671264/


All Articles