Isolated Sub-Interpreters in Python without GIL
Asked Answered
S

0

7

There are PEP-554 and PEP-684. Both are designed to support multiple interpreters on Thread level.

Does anyone know if these PEPs are implemented somewhere at least in experimental or pre-release versions of Python like 3.11?

I found out that Python 3.10 (maybe even 3.9) has these features in experimental build. If you build CPython by configuring with following flag:

./configure --with-experimental-isolated-subinterpreters

or by adding define to compile command when compiling all .c files:

#define EXPERIMENTAL_ISOLATED_SUBINTERPRETERS 1

I posted request of enabling this feature into one famous project, see issue here.

After enabling this feature as I suppose I will be able to create separate interpreters inside multiple threads (not processes), meaning that I don't need multiprocessing anymore.

More than that when using multiple Interpreters according to this feature description there is no need to have single GIL, every Interpreter in separate thread has its own GIL. It means that even if Interpreters are created inside thread, still all CPU cores are used, same like in multiprocessing. Current Python suffers from GIL only because it forces to use only single CPU core, thus multiprocessing is used by people to overcome this and use all CPU cores.

In description of these features it was said that authors had to modify by hand 1500 static and global variables by moving them all into per-thread local table inside thread state structure.

Presumably all these new features can be used right now only from Python C API.

If someone here knows how to use these isolated sub-interpreters features, can you provide some Python code or C API code with detailed example on how to use them?

Specifically I'm interested in how to use interpreters in such a way that all CPU cores are used, i.e. I want to know how to avoid single GIL, but to use multiple GILs (actually local locks, LILs). Of course I want inside threads, without using multiprocessing.

Shall answered 27/9, 2022 at 12:7 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.