销毁子解释器后释放 GIL

发布于 2024-10-24 18:37:04 字数 595 浏览 5 评论 0原文

我将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧城烟雨 2024-10-31 18:37:04

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

What happens if you call PyEval_ReleaseLock() directly after you call Py_EndInterpreter()? That's what the docs tells you to do anyway. :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文