Skip to content

3.3 Interpreter Isolation

Eric Snow edited this page May 31, 2023 · 1 revision

When it comes to the isolation of multiple interpreters in the same process, there are a number of factors to consider:

  • interpreters typically execute in different threads, but may run in the same thread
  • threads share a number of process-global resources
  • threads share the process heap and the program's data and code segments
  • each thread has its own stack, but other threads may reference memory there
  • currently most resources shared by interpreters are protected by the GIL
  • currently a process has at most one Python runtime under which all interpreter run
  • ...

The following is a deeper look at the key top-level topics related to isolation between interpreters in the same process.

Threads
Tiers of Isolation in a Process
Tiers of Isolation in CPython
Isolation Gaps
Open Questions

Threads

In many ways, isolation between interpreters is an extension of isolation between threads. So here's a brief primer.

...

Posix

...

See:

Windows

...

"Apartment" - box of thread-specific data which can only be shared between threads explicitly

See:

Other Platforms

Aside from Windows, there are a small number of operating systems (including even some *nix) that do not implement pthreads. However, either they are not supported in CPython, we handle them separately (Python/thread_*.h; none currently), or we (at least partially) simulate pthreads for them in Python/thread_pthread.h.

Python

A Python thread is an interpreter-specific wrapper around an OS-level thread. The thread-specific data stored in PyThreadState, including the ID of the corresponding OS-level thread.

...

Interestingly, the Windows docs on threads seem to agreed with Python's historical philosophy on threading:

Using multiple threads is not a guarantee of better performance.
In fact, because thread factoring is a difficult problem, using
multiple threads often causes performance problems. The key is
to use multiple threads only if you are very sure of what you
are doing.

Degrees of Isolation

...

Degrees of Isolation in a Process

global resources

  • (open) file descriptors
  • environment variables
  • signals
  • ...

See: http://man7.org/linux/man-pages/man7/pthreads.7.html#DESCRIPTION

per-thread resources

...

See: http://man7.org/linux/man-pages/man7/pthreads.7.html#DESCRIPTION

Degrees of Isolation in CPython

The Win32 docs makes a key observation:

The only global variables in the ... program are either
mutexes or variables that never change after they are initialized.

process-global data

...

runtime-global state

...

See: PyRuntimeState

interpreter-global state

...

See: PyInterpreterState

thread-global state

...

See: PyThreadState

Isolation Gaps

should be per-interpreter?

  • memory allocators / arenas (also have a global one?)
  • C-level object caches
  • freelists
  • static types

must be per-interpreter

  • objects (cannot even be used in another interpreter?)
  • modules and other interpreter-global state

extension modules

...

Open Questions

  • How are process global resources handled in other runtimes that support multiple runtimes in a single process?