Thread states and the global interpreter lock

Unless on a free-threaded build of CPython, the Python interpreter is not fully thread-safe. In order to support multi-threaded Python programs, there's a global lock, called the global interpreter lock or GIL, that must be held by the current thread before it can safely access Python objects. Without the lock, even the simplest operations could cause problems in a multi-threaded program: for example, when two threads simultaneously increment the reference count of the same object, the reference count could end up being incremented only once instead of twice.

このため、 GIL を獲得したスレッドだけが Python オブジェクトを操作したり、 Python/C API 関数を呼び出したりできるというルールがあります。並行処理をエミュレートするために、インタプリタは定期的にロックを解放したり獲得したりします。 (sys.setswitchinterval() を参照) このロックはブロックが起こりうる I/O 操作の付近でも解放・獲得され、 I/O を要求するスレッドが I/O 操作の完了を待つ間、他のスレッドが動作できるようにしています。

The Python interpreter keeps some thread-specific bookkeeping information inside a data structure called PyThreadState, known as a thread state. Each OS thread has a thread-local pointer to a PyThreadState; a thread state referenced by this pointer is considered to be attached.

A thread can only have one attached thread state at a time. An attached thread state is typically analogous with holding the GIL, except on free-threaded builds. On builds with the GIL enabled, attaching a thread state will block until the GIL can be acquired. However, even on builds with the GIL disabled, it is still required to have an attached thread state to call most of the C API.

In general, there will always be an attached thread state when using Python's C API. Only in some specific cases (such as in a Py_BEGIN_ALLOW_THREADS block) will the thread not have an attached thread state. If uncertain, check if PyThreadState_GetUnchecked() returns NULL.

Detaching the thread state from extension code

Most extension code manipulating the thread state has the following simple structure:

Save the thread state in a local variable.
... Do some blocking I/O operation ...
Restore the thread state from the local variable.

この構造は非常に一般的なので、作業を単純にするために2つのマクロが用意されています:

Py_BEGIN_ALLOW_THREADS
... Do some blocking I/O operation ...
Py_END_ALLOW_THREADS

Py_BEGIN_ALLOW_THREADS マクロは新たなブロックを開始し、隠しローカル変数を宣言します; Py_END_ALLOW_THREADS はブロックを閉じます。

上のブロックは次のコードに展開されます:

PyThreadState *_save;

_save = PyEval_SaveThread();
... Do some blocking I/O operation ...
PyEval_RestoreThread(_save);

Here is how these functions work:

The attached thread state holds the GIL for the entire interpreter. When detaching the attached thread state, the GIL is released, allowing other threads to attach a thread state to their own thread, thus getting the GIL and can start executing. The pointer to the prior attached thread state is stored as a local variable. Upon reaching Py_END_ALLOW_THREADS, the thread state that was previously attached is passed to PyEval_RestoreThread(). This function will block until another releases its thread state, thus allowing the old thread state to get re-attached and the C API can be called again.

For free-threaded builds, the GIL is normally out of the question, but detaching the thread state is still required for blocking I/O and long operations. The difference is that threads don't have to wait for the GIL to be released to attach their thread state, allowing true multi-core parallelism.

注釈

Calling system I/O functions is the most common use case for detaching the thread state, but it can also be useful before calling long-running computations which don't need access to Python objects, such as compression or cryptographic functions operating over memory buffers. For example, the standard zlib and hashlib modules detach the thread state when compressing or hashing data.

APIs

以下のマクロは、通常末尾にセミコロンを付けずに使います; Python ソース配布物内の使用例を見てください。

注釈

These macros are still necessary on the free-threaded build to prevent deadlocks.

Py_BEGIN_ALLOW_THREADS
次に属します: Stable ABI.

このマクロを展開すると { PyThreadState *_save; _save = PyEval_SaveThread(); になります。マクロに開き波括弧が入っていることに注意してください; この波括弧は後で Py_END_ALLOW_THREADS マクロと対応させなければなりません。マクロについての詳しい議論は上記を参照してください。

Py_END_ALLOW_THREADS
次に属します: Stable ABI.

このマクロを展開すると PyEval_RestoreThread(_save); } になります。マクロに開き波括弧が入っていることに注意してください; この波括弧は事前の Py_BEGIN_ALLOW_THREADS マクロと対応していなければなりません。マクロについての詳しい議論は上記を参照してください。

Py_BLOCK_THREADS
次に属します: Stable ABI.

このマクロを展開すると PyEval_RestoreThread(_save); になります: 閉じ波括弧のない Py_END_ALLOW_THREADS と同じです。

Py_UNBLOCK_THREADS
次に属します: Stable ABI.

このマクロを展開すると _save = PyEval_SaveThread(); になります: 開き波括弧のない Py_BEGIN_ALLOW_THREADS と同じです。

Python 以外で作られたスレッド

When threads are created using the dedicated Python APIs (such as the threading module), a thread state is automatically associated to them and the code shown above is therefore correct. However, when threads are created from C (for example by a third-party library with its own thread management), they don't hold the GIL, because they don't have an attached thread state.

If you need to call Python code from these threads (often this will be part of a callback API provided by the aforementioned third-party library), you must first register these threads with the interpreter by creating an attached thread state before you can start using the Python/C API. When you are done, you should detach the thread state, and finally free it.

PyGILState_Ensure()PyGILState_Release() はこの処理を自動的に行います。 Cのスレッドから Python を呼び出す典型的な方法は以下のとおりです:

PyGILState_STATE gstate;
gstate = PyGILState_Ensure();

/* Perform Python actions here. */
result = CallSomeFunction();
/* evaluate result or handle exception */

/* Release the thread. No Python API allowed beyond this point. */
PyGILState_Release(gstate);

Note that the PyGILState_* functions assume there is only one global interpreter (created automatically by Py_Initialize()). Python supports the creation of additional interpreters (using Py_NewInterpreter()), but mixing multiple interpreters and the PyGILState_* API is unsupported. This is because PyGILState_Ensure() and similar functions default to attaching a thread state for the main interpreter, meaning that the thread can't safely interact with the calling subinterpreter.

Supporting subinterpreters in non-Python threads

If you would like to support subinterpreters with non-Python created threads, you must use the PyThreadState_* API instead of the traditional PyGILState_* API.

In particular, you must store the interpreter state from the calling function and pass it to PyThreadState_New(), which will ensure that the thread state is targeting the correct interpreter:

/* The return value of PyInterpreterState_Get() from the
   function that created this thread. */
PyInterpreterState *interp = ThreadData->interp;
PyThreadState *tstate = PyThreadState_New(interp);
PyThreadState_Swap(tstate);

/* GIL of the subinterpreter is now held.
   Perform Python actions here. */
result = CallSomeFunction();
/* evaluate result or handle exception */

/* Destroy the thread state. No Python API allowed beyond this point. */
PyThreadState_Clear(tstate);
PyThreadState_DeleteCurrent();

Cautions about fork()

Another important thing to note about threads is their behaviour in the face of the C fork() call. On most systems with fork(), after a process forks only the thread that issued the fork will exist. This has a concrete impact both on how locks must be handled and on all stored state in CPython's runtime.

The fact that only the "current" thread remains means any locks held by other threads will never be released. Python solves this for os.fork() by acquiring the locks it uses internally before the fork, and releasing them afterwards. In addition, it resets any Lock objects in the child. When extending or embedding Python, there is no way to inform Python of additional (non-Python) locks that need to be acquired before or reset after a fork. OS facilities such as pthread_atfork() would need to be used to accomplish the same thing. Additionally, when extending or embedding Python, calling fork() directly rather than through os.fork() (and returning to or calling into Python) may result in a deadlock by one of Python's internal locks being held by a thread that is defunct after the fork. PyOS_AfterFork_Child() tries to reset the necessary locks, but is not always able to.

The fact that all other threads go away also means that CPython's runtime state there must be cleaned up properly, which os.fork() does. This means finalizing all other PyThreadState objects belonging to the current interpreter and all other PyInterpreterState objects. Due to this and the special nature of the "main" interpreter, fork() should only be called in that interpreter's "main" thread, where the CPython global runtime was originally initialized. The only exception is if exec() will be called immediately after.

高水準 API

These are the most commonly used types and functions when writing multi-threaded C extensions.

type PyThreadState
次に属します: Limited API (不透明な構造体として).

This data structure represents the state of a single thread. The only public data member is:

PyInterpreterState *interp

This thread's interpreter state.

void PyEval_InitThreads()
次に属します: Stable ABI.

Deprecated function which does nothing.

In Python 3.6 and older, this function created the GIL if it didn't exist.

バージョン 3.9 で変更: The function now does nothing.

バージョン 3.7 で変更: この関数は Py_Initialize() から呼び出されるようになり、わざわざ呼び出す必要はもう無くなりました。

バージョン 3.2 で変更: この関数は Py_Initialize() より前に呼び出すことができなくなりました。

バージョン 3.9 で非推奨.

PyThreadState *PyEval_SaveThread()
次に属します: Stable ABI.

Detach the attached thread state and return it. The thread will have no thread state upon returning.

void PyEval_RestoreThread(PyThreadState *tstate)
次に属します: Stable ABI.

Set the attached thread state to tstate. The passed thread state should not be attached, otherwise deadlock ensues. tstate will be attached upon returning.

注釈

Calling this function from a thread when the runtime is finalizing will hang the thread until the program exits, even if the thread was not created by Python. Refer to Cautions regarding runtime finalization for more details.

バージョン 3.14 で変更: Hangs the current thread, rather than terminating it, if called while the interpreter is finalizing.

PyThreadState *PyThreadState_Get()
次に属します: Stable ABI.

Return the attached thread state. If the thread has no attached thread state, (such as when inside of Py_BEGIN_ALLOW_THREADS block), then this issues a fatal error (so that the caller needn't check for NULL).

See also PyThreadState_GetUnchecked().

PyThreadState *PyThreadState_GetUnchecked()

Similar to PyThreadState_Get(), but don't kill the process with a fatal error if it is NULL. The caller is responsible to check if the result is NULL.

Added in version 3.13: In Python 3.5 to 3.12, the function was private and known as _PyThreadState_UncheckedGet().

PyThreadState *PyThreadState_Swap(PyThreadState *tstate)
次に属します: Stable ABI.

Set the attached thread state to tstate, and return the thread state that was attached prior to calling.

This function is safe to call without an attached thread state; it will simply return NULL indicating that there was no prior thread state.

注釈

Similar to PyGILState_Ensure(), this function will hang the thread if the runtime is finalizing.

GIL-state APIs

以下の関数はスレッドローカルストレージを利用していて、サブインタプリタとの互換性がありません:

type PyGILState_STATE
次に属します: Stable ABI.

The type of the value returned by PyGILState_Ensure() and passed to PyGILState_Release().

enumerator PyGILState_LOCKED

The GIL was already held when PyGILState_Ensure() was called.

enumerator PyGILState_UNLOCKED

The GIL was not held when PyGILState_Ensure() was called.

PyGILState_STATE PyGILState_Ensure()
次に属します: Stable ABI.

Ensure that the current thread is ready to call the Python C API regardless of the current state of Python, or of the attached thread state. This may be called as many times as desired by a thread as long as each call is matched with a call to PyGILState_Release(). In general, other thread-related APIs may be used between PyGILState_Ensure() and PyGILState_Release() calls as long as the thread state is restored to its previous state before the Release(). For example, normal usage of the Py_BEGIN_ALLOW_THREADS and Py_END_ALLOW_THREADS macros is acceptable.

The return value is an opaque "handle" to the attached thread state when PyGILState_Ensure() was called, and must be passed to PyGILState_Release() to ensure Python is left in the same state. Even though recursive calls are allowed, these handles cannot be shared - each unique call to PyGILState_Ensure() must save the handle for its call to PyGILState_Release().

When the function returns, there will be an attached thread state and the thread will be able to call arbitrary Python code. Failure is a fatal error.

警告

Calling this function when the runtime is finalizing is unsafe. Doing so will either hang the thread until the program ends, or fully crash the interpreter in rare cases. Refer to Cautions regarding runtime finalization for more details.

バージョン 3.14 で変更: Hangs the current thread, rather than terminating it, if called while the interpreter is finalizing.

void PyGILState_Release(PyGILState_STATE)
次に属します: Stable ABI.

獲得したすべてのリソースを解放します。この関数を呼び出すと、Pythonの状態は対応する PyGILState_Ensure() を呼び出す前と同じとなります (通常、この状態は呼び出し元でははわかりませんので、GILState APIを利用するようにしてください)。

PyGILState_Ensure() を呼び出す場合は、必ず同一スレッド内で対応する PyGILState_Release() を呼び出してください。

PyThreadState *PyGILState_GetThisThreadState()
次に属します: Stable ABI.

Get the attached thread state for this thread. May return NULL if no GILState API has been used on the current thread. Note that the main thread always has such a thread-state, even if no auto-thread-state call has been made on the main thread. This is mainly a helper/diagnostic function.

注釈

This function may return non-NULL even when the thread state is detached. Prefer PyThreadState_Get() or PyThreadState_GetUnchecked() for most cases.

int PyGILState_Check()

Return 1 if the current thread is holding the GIL and 0 otherwise. This function can be called from any thread at any time. Only if it has had its thread state initialized via PyGILState_Ensure() will it return 1. This is mainly a helper/diagnostic function. It can be useful for example in callback contexts or memory allocation functions when knowing that the GIL is locked can allow the caller to perform sensitive actions or otherwise behave differently.

注釈

If the current Python process has ever created a subinterpreter, this function will always return 1. Prefer PyThreadState_GetUnchecked() for most cases.

Added in version 3.4.

低水準 API

PyThreadState *PyThreadState_New(PyInterpreterState *interp)
次に属します: Stable ABI.

Create a new thread state object belonging to the given interpreter object. An attached thread state is not needed.

void PyThreadState_Clear(PyThreadState *tstate)
次に属します: Stable ABI.

Reset all information in a thread state object. tstate must be attached

バージョン 3.9 で変更: This function now calls the PyThreadState.on_delete callback. Previously, that happened in PyThreadState_Delete().

バージョン 3.13 で変更: The PyThreadState.on_delete callback was removed.

void PyThreadState_Delete(PyThreadState *tstate)
次に属します: Stable ABI.

Destroy a thread state object. tstate should not be attached to any thread. tstate must have been reset with a previous call to PyThreadState_Clear().

void PyThreadState_DeleteCurrent(void)

Detach the attached thread state (which must have been reset with a previous call to PyThreadState_Clear()) and then destroy it.

No thread state will be attached upon returning.

PyFrameObject *PyThreadState_GetFrame(PyThreadState *tstate)
次に属します: Stable ABI (バージョン 3.10 より).

Get the current frame of the Python thread state tstate.

Return a strong reference. Return NULL if no frame is currently executing.

See also PyEval_GetFrame().

tstate must not be NULL, and must be attached.

Added in version 3.9.

uint64_t PyThreadState_GetID(PyThreadState *tstate)
次に属します: Stable ABI (バージョン 3.10 より).

Get the unique thread state identifier of the Python thread state tstate.

tstate must not be NULL, and must be attached.

Added in version 3.9.

PyInterpreterState *PyThreadState_GetInterpreter(PyThreadState *tstate)
次に属します: Stable ABI (バージョン 3.10 より).

Get the interpreter of the Python thread state tstate.

tstate must not be NULL, and must be attached.

Added in version 3.9.

void PyThreadState_EnterTracing(PyThreadState *tstate)

Suspend tracing and profiling in the Python thread state tstate.

Resume them using the PyThreadState_LeaveTracing() function.

Added in version 3.11.

void PyThreadState_LeaveTracing(PyThreadState *tstate)

Resume tracing and profiling in the Python thread state tstate suspended by the PyThreadState_EnterTracing() function.

See also PyEval_SetTrace() and PyEval_SetProfile() functions.

Added in version 3.11.

int PyUnstable_ThreadState_SetStackProtection(PyThreadState *tstate, void *stack_start_addr, size_t stack_size)
これは Unstable APIです。マイナーリリースで予告なく変更されることがあります。

Set the stack protection start address and stack protection size of a Python thread state.

On success, return 0. On failure, set an exception and return -1.

CPython implements recursion control for C code by raising RecursionError when it notices that the machine execution stack is close to overflow. See for example the Py_EnterRecursiveCall() function. For this, it needs to know the location of the current thread's stack, which it normally gets from the operating system. When the stack is changed, for example using context switching techniques like the Boost library's boost::context, you must call PyUnstable_ThreadState_SetStackProtection() to inform CPython of the change.

Call PyUnstable_ThreadState_SetStackProtection() either before or after changing the stack. Do not call any other Python C API between the call and the stack change.

See PyUnstable_ThreadState_ResetStackProtection() for undoing this operation.

Added in version 3.15.

void PyUnstable_ThreadState_ResetStackProtection(PyThreadState *tstate)
これは Unstable APIです。マイナーリリースで予告なく変更されることがあります。

Reset the stack protection start address and stack protection size of a Python thread state to the operating system defaults.

See PyUnstable_ThreadState_SetStackProtection() for an explanation.

Added in version 3.15.

PyObject *PyThreadState_GetDict()
戻り値: 借用参照。 次に属します: Stable ABI.

Return a dictionary in which extensions can store thread-specific state information. Each extension should use a unique key to use to store state in the dictionary. It is okay to call this function when no thread state is attached. If this function returns NULL, no exception has been raised and the caller should assume no thread state is attached.

void PyEval_AcquireThread(PyThreadState *tstate)
次に属します: Stable ABI.

Attach tstate to the current thread, which must not be NULL or already attached.

The calling thread must not already have an attached thread state.

注釈

Calling this function from a thread when the runtime is finalizing will hang the thread until the program exits, even if the thread was not created by Python. Refer to Cautions regarding runtime finalization for more details.

バージョン 3.8 で変更: Updated to be consistent with PyEval_RestoreThread(), Py_END_ALLOW_THREADS(), and PyGILState_Ensure(), and terminate the current thread if called while the interpreter is finalizing.

バージョン 3.14 で変更: Hangs the current thread, rather than terminating it, if called while the interpreter is finalizing.

PyEval_RestoreThread() はいつでも (スレッドが初期化されたいないときでも) 利用可能な高レベル関数です。

void PyEval_ReleaseThread(PyThreadState *tstate)
次に属します: Stable ABI.

Detach the attached thread state. The tstate argument, which must not be NULL, is only used to check that it represents the attached thread state --- if it isn't, a fatal error is reported.

PyEval_SaveThread() はより高レベルな関数で常に (スレッドが初期化されていないときでも) 利用できます。

Asynchronous notifications

インタプリタのメインスレッドに非同期な通知を行うために提供されている仕組みです。これらの通知は関数ポインタと void ポインタ引数という形態を取ります。

int Py_AddPendingCall(int (*func)(void*), void *arg)
次に属します: Stable ABI.

インタプリタのメインスレッドから関数が呼び出される予定を組みます。成功すると 0 が返り、func はメインスレッドの呼び出しキューに詰められます。失敗すると、例外をセットせずに -1 が返ります。

無事にキューに詰められると、funcいつかは必ず インタプリタのメインスレッドから、arg を引数として呼び出されます。この関数は、通常の実行中の Python コードに対して非同期に呼び出されますが、次の両方の条件に合致したときに呼び出されます:

func must return 0 on success, or -1 on failure with an exception set. func won't be interrupted to perform another asynchronous notification recursively, but it can still be interrupted to switch threads if the thread state is detached.

This function doesn't need an attached thread state. However, to call this function in a subinterpreter, the caller must have an attached thread state. Otherwise, the function func can be scheduled to be called from the wrong interpreter.

警告

これは、非常に特別な場合にのみ役立つ、低レベルな関数です。 func が可能な限り早く呼び出される保証はありません。メインスレッドがシステムコールを実行するのに忙しい場合は、 func はシステムコールが返ってくるまで呼び出されないでしょう。この関数は一般的には、任意の C スレッドから Python コードを呼び出すのには 向きません 。これの代わりに、 PyGILState API を使用してください。

Added in version 3.1.

バージョン 3.9 で変更: If this function is called in a subinterpreter, the function func is now scheduled to be called from the subinterpreter, rather than being called from the main interpreter. Each subinterpreter now has its own list of scheduled calls.

バージョン 3.12 で変更: This function now always schedules func to be run in the main interpreter.

int Py_MakePendingCalls(void)
次に属します: Stable ABI.

Execute all pending calls. This is usually executed automatically by the interpreter.

This function returns 0 on success, and returns -1 with an exception set on failure.

If this is not called in the main thread of the main interpreter, this function does nothing and returns 0. The caller must hold an attached thread state.

Added in version 3.1.

バージョン 3.12 で変更: This function only runs pending calls in the main interpreter.

int PyThreadState_SetAsyncExc(unsigned long id, PyObject *exc)
次に属します: Stable ABI.

Asynchronously raise an exception in a thread. The id argument is the thread id of the target thread; exc is the exception object to be raised. This function does not steal any references to exc. To prevent naive misuse, you must write your own C extension to call this. Must be called with an attached thread state. Returns the number of thread states modified; this is normally one, but will be zero if the thread id isn't found. If exc is NULL, the pending exception (if any) for the thread is cleared. This raises no exceptions.

バージョン 3.7 で変更: id 引数の型が long から unsigned long へ変更されました。

Operating system thread APIs

PYTHREAD_INVALID_THREAD_ID

Sentinel value for an invalid thread ID.

This is currently equivalent to (unsigned long)-1.

unsigned long PyThread_start_new_thread(void (*func)(void*), void *arg)
次に属します: Stable ABI.

Start function func in a new thread with argument arg. The resulting thread is not intended to be joined.

func must not be NULL, but arg may be NULL.

On success, this function returns the identifier of the new thread; on failure, this returns PYTHREAD_INVALID_THREAD_ID.

The caller does not need to hold an attached thread state.

unsigned long PyThread_get_thread_ident(void)
次に属します: Stable ABI.

Return the identifier of the current thread, which will never be zero.

This function cannot fail, and the caller does not need to hold an attached thread state.

PyObject *PyThread_GetInfo(void)
次に属します: Stable ABI (バージョン 3.3 より).

Get general information about the current thread in the form of a struct sequence object. This information is accessible as sys.thread_info in Python.

On success, this returns a new strong reference to the thread information; on failure, this returns NULL with an exception set.

The caller must hold an attached thread state.

PY_HAVE_THREAD_NATIVE_ID

This macro is defined when the system supports native thread IDs.

unsigned long PyThread_get_thread_native_id(void)
次に属します: Stable ABI on platforms with native thread IDs.

Get the native identifier of the current thread as it was assigned by the operating system's kernel, which will never be less than zero.

This function is only available when PY_HAVE_THREAD_NATIVE_ID is defined.

This function cannot fail, and the caller does not need to hold an attached thread state.

void PyThread_exit_thread(void)
次に属します: Stable ABI.

Terminate the current thread. This function is generally considered unsafe and should be avoided. It is kept solely for backwards compatibility.

This function is only safe to call if all functions in the full call stack are written to safely allow it.

警告

If the current system uses POSIX threads (also known as "pthreads"), this calls pthread_exit(3), which attempts to unwind the stack and call C++ destructors on some libc implementations. However, if a noexcept function is reached, it may terminate the process. Other systems, such as macOS, do unwinding.

On Windows, this function calls _endthreadex(), which kills the thread without calling C++ destructors.

In any case, there is a risk of corruption on the thread's stack.

バージョン 3.14 で非推奨.

void PyThread_init_thread(void)
次に属します: Stable ABI.

Initialize PyThread* APIs. Python executes this function automatically, so there's little need to call it from an extension module.

int PyThread_set_stacksize(size_t size)
次に属します: Stable ABI.

Set the stack size of the current thread to size bytes.

This function returns 0 on success, -1 if size is invalid, or -2 if the system does not support changing the stack size. This function does not set exceptions.

The caller does not need to hold an attached thread state.

size_t PyThread_get_stacksize(void)
次に属します: Stable ABI.

Return the stack size of the current thread in bytes, or 0 if the system's default stack size is in use.

The caller does not need to hold an attached thread state.