From the docs:
setswitchinterval
Set the interpreter’s thread switch interval (in seconds). This floating-point value determines the ideal duration of the “timeslices” allocated to concurrently running Python threads. Please note that the actual value can be higher, especially if long-running internal functions or methods are used. Also, which thread becomes scheduled at the end of the interval is the operating system’s decision. The interpreter doesn’t have its own scheduler.
(getswitchinterval()
retrieves the value. The standard value is 0.005 seconds).
This dates from Python 3.2, in 2009. I read here that this mechanism was introduced originally to prevent CPU-bound threads hogging the GIL.
This problem was fixed in Python 3.2 in 2009 by Antoine Pitrou who added a mechanism of looking at the number of GIL acquisition requests by other threads that got dropped and not allowing the current thread to reacquire GIL before other threads got a chance to run.
Originally it was called sys.setcheckinterval()
/sys.getcheckinterval()
(now both deprecated) and gave a number of machine instructions rather than a time interval.
In PyQt, when you have an intensive worker thread and you want to prevent that freezing the GUI in the event thread, you have to introduce time.sleep(...)
calls (there may be other mechanisms). In my experience, without this a worker thread (QThread
) will simply never let go until it finishes.
But does anyone know why this Python 3.2+ "switch interval" mechanism doesn't provide a way of "letting the event thread breathe", out of the box? I'm guessing that the answer may be that PyQt sets up its own thread-switching architecture which overrides the basic Python one. But if that's the case it seems odd that something as basic as time.sleep(..)
should "fix" the problem of providing event thread responsiveness. Obviously time.sleep(..)
is not doing the same job as this switch interval mechanism: but in what way then is it different, at least in a PyQt context?
Incidentally, I'm using PyQt5... I presume it's the same with later versions.