Skip to content

Commit b1d6ed7

Browse files
Clarify about platform support for threads.
1 parent 3f3d5cc commit b1d6ed7

1 file changed

Lines changed: 24 additions & 17 deletions

File tree

Doc/reference/executionmodel.rst

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -416,14 +416,19 @@ on the computer look something like this::
416416
417417
While a program always starts with exactly one of each of those, it may
418418
grow to include multiple of each. Hosts and processes are isolated and
419-
independent from one another. However, threads are not. Each thread
420-
does *run* independently, for the small segments of time it is
421-
scheduled to execute its code on the CPU. Otherwise, all threads
419+
independent from one another. However, threads are not.
420+
421+
Not all platforms support threads, though most do. For those that do,
422+
each thread does *run* independently, for the small segments of time it
423+
is scheduled to execute its code on the CPU. Otherwise, all threads
422424
in a process share all the process' resources, including memory.
423-
This is exactly what can make threads a pain: two threads running
424-
at the same arbitrary time on different CPU cores can accidentally
425-
interfere with each other's use of some shared data. The initial
426-
thread is known as the "main" thread.
425+
The initial thread is known as the "main" thread.
426+
427+
.. note::
428+
429+
The way they share resources is exactly what can make threads a pain:
430+
two threads running at the same arbitrary time on different CPU cores
431+
can accidentally interfere with each other's use of some shared data.
427432

428433
The same layers apply to each Python program, with some extra layers
429434
specific to Python::
@@ -435,20 +440,21 @@ specific to Python::
435440
Python thread (runs bytecode)
436441

437442
When a Python program starts, it looks exactly like that, with one
438-
of each. The process has a single global runtime to manage global
439-
resources. Each Python thread has all the state it needs to run
443+
of each. The process has a single global runtime to manage Python's
444+
global resources. Each Python thread has all the state it needs to run
440445
Python code (and use any supported C-API) in its OS thread.
441446

442447
.. , including its stack of call frames.
443448
444449
.. If the program uses coroutines (async) then the thread will end up
445450
juggling multiple stacks.
446451
447-
In between the global runtime and the threads lies the interpreter.
448-
It encapsulates all of the non-global runtime state that the
449-
interpreter's Python threads share. For example, all those threads
450-
share :data:`sys.modules`. When a Python thread is created, it belongs
451-
to an interpreter.
452+
In between the global runtime and the thread(s) lies the interpreter.
453+
It completely encapsulates all of the non-global runtime state that the
454+
interpreter's Python threads share. For example, all its threads share
455+
:data:`sys.modules`. When a Python thread is created, it belongs
456+
to an interpreter, and likewise when an OS thread is otherwise
457+
associated with Python.
452458

453459
If the runtime supports using multiple interpreters then each OS thread
454460
will have at most one Python thread for each interpreter. However,
@@ -460,9 +466,10 @@ The initial interpreter is known as the "main" interpreter.
460466
of which each thread has one to execute Python code.)
461467
462468
Once a program is running, new Python threads can be created using the
463-
:mod:`threading` module. Additional processes can be created using the
464-
:mod:`multiprocessing` and :mod:`subprocess` modules. You can run
465-
coroutines (async) in the main thread using :mod:`asyncio`.
469+
:mod:`threading` module (on platforms and Python implementations that
470+
support threads). Additional processes can be created using the
471+
:mod:`os`, :mod:`subprocess`, and :mod:`multiprocessing` modules.
472+
You can run coroutines (async) in the main thread using :mod:`asyncio`.
466473
Interpreters can be created using the :mod:`concurrent.interpreters`
467474
module.
468475

0 commit comments

Comments
 (0)