Asynchronous notifications¶
A mechanism is provided to make asynchronous notifications to the main interpreter thread. These notifications take the form of a function pointer and a void pointer argument.
- 為 穩定 ABI 的一部分.
Schedule a function to be called from the main interpreter thread. On success,
0is returned and func is queued for being called in the main thread. On failure,-1is returned without setting any exception.When successfully queued, func will be eventually called from the main interpreter thread with the argument arg. It will be called asynchronously with respect to normally running Python code, but with both these conditions met:
on a bytecode boundary;
with the main thread holding an attached thread state (func can therefore use the full C API).
func must return
0on success, or-1on 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.
警告
This is a low-level function, only useful for very special cases. There is no guarantee that func will be called as quick as possible. If the main thread is busy executing a system call, func won't be called before the system call returns. This function is generally not suitable for calling Python code from arbitrary C threads. Instead, use the PyGILState API.
在 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)¶
- 為 穩定 ABI 的一部分.
Execute all pending calls. This is usually executed automatically by the interpreter.
This function returns
0on success, and returns-1with 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.在 3.1 版被加入.
在 3.12 版的變更: This function only runs pending calls in the main interpreter.
- 為 穩定 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 版的變更: The type of the id parameter changed from long to unsigned long.