New submission from Xavier Morel <xavier.mo...@masklinn.net>:

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.

----------
components: Interpreter Core
messages: 393951
nosy: xmorel
priority: normal
severity: normal
status: open
title: Add an interpreter-level critical section construct
type: enhancement
versions: Python 3.9

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue44178>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to