简体   繁体   English

boost.python解释器实例化

[英]boost.python interpreter instantiation

I need to use boost::python , but I want the interpreter context to be destroyed after the python interaction has finished. 我需要使用boost::python ,但是我想在python交互完成后销毁解释器上下文。

How and when exactly is the python interpreter being instantiated, and how do I destroy all the resources allocated by it? 实例化python解释器的方式和时间,以及如何销毁它分配的所有资源?

Update Apparently Py_Finalize is not designed to be called many times in the same process. 显然, 更新 Py_Finalize不能在同一过程中多次调用。 It seems this approach won't work: http://docs.python.org/2/c-api/init.html#l2h-652 看来这种方法行不通: http : //docs.python.org/2/c-api/init.html#l2h-652

Py_Finalize is designed to only be called once per process. Py_Finalize设计为每个进程仅被调用一次。 The way I handled it is to put it into the shutdown hook for our application, or run it at the end of the main function. 我处理它的方法是将其放入应用程序的关闭挂钩中,或者在主要功能的末尾运行它。 You can have multiple instances of the interpreter context and those get cleaned up when their "__main__" object (the one created by import("__main__") ) is destroyed. 您可以具有解释器上下文的多个实例,并且销毁了它们的“ __main__”对象(由import(“ __ main__”)创建的对象)后,这些实例就会被清理。

Boost.Python does not support safely calling Py_Finalize() . Boost.Python不支持安全地调用Py_Finalize()

Per the Embedding - Getting started section: 根据“ 嵌入-入门”部分:

Note that at this time you must not call Py_Finalize() to stop the interpreter. 请注意,此时您不得调用Py_Finalize()来停止解释器。 This may be fixed in a future version of boost.python. 这可能会在boost.python的未来版本中修复。

Additional details are noted in the TODO . 其他细节在TODO中有说明。 In short, some internal Boost.Python objects will remain alive during Py_Finalize() , and only attempt to be deleted when Boost.Python unloads, causing the objects to attempt deletion with a non-existent interpreter. 简而言之,一些内部Boost.Python对象将在Py_Finalize()期间保持活动状态,并且仅在Boost.Python卸载时尝试删除,从而导致这些对象尝试使用不存在的解释器进行删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM