This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Add an interpreter-level critical section construct
Type: enhancement Stage:
Components: Interpreter Core Versions: Python 3.9
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: xmorel
Priority: normal Keywords:

Created on 2021-05-19 12:18 by xmorel, last changed 2022-04-11 14:59 by admin.

Messages (1)
msg393951 - (view) Author: Xavier Morel (xmorel) * Date: 2021-05-19 12:18
Python code uses a fair amount of globals, and sometimes there's no good choice but to do dodgy things and temporarily update global state for one reason or another e.g. redirect a standard fd or stream (cf redirect_stdout), monkey-patch a builtin to intercept some behaviour, etc...

One issue with this is, if the program is multithreaded there is no real way to prevent the interpreter from suspending the current thread *while doing the dodgy thing*. The only interpreter-level global lock would be the GIL, and while native extension can just not release the GIL this is not an option for Python code (as far as I know).

`sys` does provide getswitchinterval()/setswitchinterval() (and under the old GIL had getcheckinterval()/setcheckinterval()), but the semantics of these functions is a bit obscure (e.g. does `sys.setcheckinterval` update the *current* time slice os is the current thread doomed? can it fail? what happens if it does?) and they feel extremely uncomfortable.

As such, having a context manager which would explicitly but only prevent thread switching while it's held would be valuable. Assuming `setswitchinginterval` operates on the "current" timeslice, this would not provide any more misbehaving powers as any code can already call `sys.setswitchinterval(sys.maxsize)` (unless there are already mitigation systems in-place), and the contextmanager could just do that internally if that is the "correct" way to prevent thread switching under the current scheme.

The one thing which is not great is that this scheme might not really work under GILectomy, but then neither does switchinterval I think.
History
Date User Action Args
2022-04-11 14:59:45adminsetgithub: 88344
2021-05-19 12:18:33xmorelcreate