销毁子解释器后释放 GIL
我将 Python 3.2 嵌入到 C++ 应用程序中,并且有几个在程序中不同时间运行的子解释器(由 Py_NewInterpreter
创建)。他们在不同的时间获取和释放 GIL,但是当我想销毁其中一个子解释器时遇到了问题。
要销毁子解释器,您必须获取 GIL。所以我这样做:
PyEval_AcquireLock(threadstate);
然后我用 销毁解释器
Py_EndInterpreter(threadstate);
你会认为它会释放 GIL 因为保存它的东西被销毁了。然而,Py_EndInterpreter
的文档说:
给定的线程状态必须是 当前线程状态。请参阅 下面讨论线程状态。 当调用返回时,当前 线程状态为NULL。 (全局解释器锁必须在调用此函数之前保持,并且在返回时仍然保持。)
因此,如果我在销毁子解释器时必须保持 GIL,并销毁子解释器,则将其设置为 NULL,并且我必须拥有获取GIL的线程要释放它,销毁子解释器后如何释放GIL?
I am embedding Python 3.2 in a C++ application and I have several sub interpreters that run at various times in the programs (created by Py_NewInterpreter
). They acquire and release the GIL at various times, but I have run into a problem when I want to destroy one of the sub interpreters.
To destroy a sub interpreter, you have to acquire the GIL. So I do this:
PyEval_AcquireLock(threadstate);
Then I destroy the interpreter with
Py_EndInterpreter(threadstate);
And you would think it would release the GIL because the thing that held it was destroyed. However, the documentation for Py_EndInterpreter
says:
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.)
So if I have to hold the GIL when I destroy a sub interpreter and destroying the sub interpreter sets it to NULL and I have to have the thread that acquired the GIL to release it, how do I release the GIL after destroying a sub-interpreter?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果在调用
Py_EndInterpreter()
后直接调用PyEval_ReleaseLock()
会发生什么?无论如何,这就是文档告诉你要做的事情。 :)What happens if you call
PyEval_ReleaseLock()
directly after you callPy_EndInterpreter()
? That's what the docs tells you to do anyway. :)