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.

This post was flagged by the community and is temporarily hidden.

OT, but to avoid spam and off-topic questions, might it be a good idea to limit core dev to TL1 or TL2 users (unless they have some a special role or request it)? Seems like the great majority of OT posts and comments are by brand new/TL0 users, and maybe TL1s, while I’m not sure of any I’ve seen that are on-topic that are below TL2 (or maaaaybe TL1). If e.g. a new contributor to CPython dev had a genuine need to post, they could always ask a mod/admin/etc to bump them.