gpt4 book ai didi

python - 销毁子解释器后释放 GIL

转载 作者:行者123 更新时间:2023-11-28 17:53:27 26 4
gpt4 key购买 nike

我将 Python 3.2 嵌入到 C++ 应用程序中,并且我有几个在程序中不同时间运行的子解释器(由 Py_NewInterpreter 创建)。他们在不同时间获取和释放 GIL,但当我想销毁其中一个子解释器时遇到了问题。

要销毁子解释器,您必须获得 GIL。所以我这样做:

PyEval_AcquireLock(threadstate);

然后我销毁解释器

Py_EndInterpreter(threadstate);

你会认为它会释放 GIL,因为持有它的东西被摧毁了。但是,Py_EndInterpreter 的文档说:

The given thread state must be the current thread state. See the discussion of thread states below. When the call returns, the current thread state is NULL. (The global interpreter lock must be held before calling this function and is still held when it returns.)

因此,如果我在销毁子解释器时必须持有 GIL,并且销毁子解释器会将其设置为 NULL,并且我必须让获取 GIL 的线程释放它,那么在销毁一个子解释器后如何释放 GIL副翻译?

最佳答案

如果在调用 Py_EndInterpreter() 之后直接调用 PyEval_ReleaseLock() 会发生什么情况?无论如何,这就是文档告诉您要做的。 :)

关于python - 销毁子解释器后释放 GIL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5371910/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com