Python Context Switching - how is it done?

I read that Python3 uses a strict timer for context switching between threads. What I am wondering is, how do Python3 interpreters do that succesfully? From what I understand it’s basically impossible to progmatically force timer based context-switching for programs running on an OS.

Specifically the CPython implementation, and not just Python V3, but V2 as well. The OS still manages context-switching between threads of execution on each thread runner (ie CPU core), but what you might be referring to is the current thread allowed to execute: the global interpreter lock allows only one thread to run Python byte code at any given time, and this lock is released after 100 executions.

If you’re talking about coroutines, the context of switched when something is awaited on, and I think this is handled by the event loop.

PS: not really a discussion relevant to core-development (even though it’s about the core internals of the runtime). Maybe users?

My understanding is that in python 3 it’s timer based and not based upon byte-code executed, is this correct? I still don’t understand how this works since an OS function could hang.

Is basically a condition variable + a timed wait. Notice the switching is not strict, as is still done on eval loop passes. This means that if a thread is doing something in native code, the switching may take much more than the default interval.