@@ -506,8 +506,7 @@ multiple thread states.
506
506
As a whole, the Python runtime consists of the global runtime state,
507
507
interpreters, and thread states. The runtime ensures all that state
508
508
stays consistent over its lifetime, particularly when used with
509
- multiple host threads. The runtime also exposes a way for host threads
510
- to "call into Python", which will be covered in the next subsection.
509
+ multiple host threads.
511
510
512
511
The global runtime, at the conceptual level, is just a set of
513
512
interpreters. While they are otherwise isolated and independent from
@@ -563,7 +562,9 @@ call stack. It may include other thread-specific resources.
563
562
Each thread state, over its lifetime, is always tied to exactly one
564
563
interpreter and exactly one host thread. It will only ever be used in
565
564
that thread. In the other direction, a host thread may have many
566
- Python thread states tied to it, for different interpreters.
565
+ Python thread states tied to it, for different interpreters or even the
566
+ same interpreter. However, for any given host thread, only one of the
567
+ thread states tied to it can be used by the thread at a time.
567
568
568
569
Once a program is running, new Python threads can be created using the
569
570
:mod: `threading ` module (on platforms and Python implementations that
@@ -573,41 +574,6 @@ You can run coroutines (async) in the main thread using :mod:`asyncio`.
573
574
Interpreters can be created and used with the
574
575
:mod: `~concurrent.interpreters ` module.
575
576
576
- Calls into Python
577
- -----------------
578
-
579
- A "call into Python" is an abstraction of "ask the Python runtime
580
- to do something". It necessarily involves targeting a single runtime
581
- context, whether global, interpreter, or thread. The layer depends
582
- on the desired operation. Most operations require a thread state.
583
-
584
- When a running host thread calls into Python, the actual mechanism
585
- is implementation-specific. For example, CPython provides a C-API and
586
- the thread will literally call into Python through a C-API function.
587
-
588
- .. drop paragraph?
589
-
590
- Some thread-specific operations must only target a new thread state,
591
- while others may target any thread state, including one with a Python
592
- call already on its stack or a current exception set.
593
-
594
- A thread-specific call into Python can target only one thread state.
595
- That means, when there are multiple Python thread states tied to the
596
- current host thread, only one of them can be in use at a time. It
597
- doesn't matter if the thread states belong to different interpreters
598
- or the same interpreter.
599
-
600
- Calls into Python can be nested. Even if a thread has already called
601
- into Python, that operation could be interrupted by another call into
602
- Python targeting a different runtime context. For example, the
603
- implementation of the outer call might make the inner call directly.
604
- Alternately, the host or Python runtime might trigger some
605
- asyncronous callback that calls into Python.
606
-
607
- Regardless, at the point of the inner call, the target is swapped.
608
- When the inner call finishes, the target is swapped back and the outer
609
- call resumes.
610
-
611
577
612
578
.. rubric :: Footnotes
613
579
0 commit comments