简体   繁体   English

Py_Initialize和Py_finalize以及MatPlotlib

[英]Py_Initialize and Py_finalize and MatPlotlib

This is a known problem, but I want to ask the experts for the best way to solve it for me. 这是一个众所周知的问题,但我想请专家为我解决这个问题的最佳方法。

I have a project (Euler Math Toolbox), which runs Python as a script language. 我有一个项目(Euler Math Toolbox),它将Python作为脚本语言运行。 For this, a library module "python.dll" is loaded at run time, which is linked against "python27.lib". 为此,在运行时加载库模块“python.dll”,该模块与“python27.lib”链接。 Then Py_Initialize is called. 然后调用Py_Initialize。 This all works well. 这一切都运作良好。

But Euler can be restarted by the user with a new session and notebook. 但是,用户可以使用新会话和笔记本重新启动Euler。 Then I want Python to clear all variables and imports. 然后我希望Python清除所有变量和导入。 For this, I call Py_Finalize and unload "python.dll". 为此,我调用Py_Finalize并卸载“python.dll”。 When Python is needed, loading and initializing starts Python again. 当需要Python时,加载和初始化会再次启动Python。

This works. 这有效。 But Python crashes at the first call, if MatPlotlib is imported in the previous session. 但是,如果在上一个会话中导入了MatPlotlib,Python会在第一次调用时崩溃。 It seems that Py_Finalize does not completely clear Python, nor does unloading my "python.dll". 似乎Py_Finalize并没有完全清除Python,也没有卸载我的“python.dll”。 I tried unloading "python27.dll" (the Python DLL), but this does not help. 我试过卸载“python27.dll”(Python DLL),但这没有帮助。 Most likey, another DLL remains active, but corrupts during Py_Finalize. 最喜欢的是,另一个DLL保持活动状态,但在Py_Finalize期间会破坏。

To solve this, it would suffice to clear all variables and imports. 要解决这个问题,清除所有变量和导入就足够了。 I could live with not calling Py_Finalize. 我可以忍住不叫Py_Finalize。 But how? 但是怎么样?

PS: You may wonder, why I do not directly link euler.exe to Python. PS:您可能想知道,为什么我不直接将euler.exe链接到Python。 The reason is that this prevents Euler form starting, if Python is not installed, even if it is never needed. 原因是如果没有安装Python,这会阻止Euler表单启动,即使从不需要它。

Thanks for any answers! 谢谢你的回答! You duplicate your answer to renegrothmann at gmail, if you like. 如果你愿意,你可以在gmail上复制你对renegrothmann的回答。 That would help me. 那会对我有所帮助。

To solve this, it would suffice to clear all variables and imports. 要解决这个问题,清除所有变量和导入就足够了。 I could live with not calling Py_Finalize. 我可以忍住不叫Py_Finalize。 But how? 但是怎么样?

Provided you properly release all references after each call, this should work fine. 如果您在每次调用后正确释放所有引用,这应该可以正常工作。 Just make sure to only call Py_Initialize a single time, and never call Py_Finalize . 只需确保只调用Py_Initialize ,并且永远不要调用Py_Finalize Run each "session" using a separate dictionary, and always decrement the reference counts properly when you're done with them (which will release those variables after running your code). 使用单独的字典运行每个“会话”,并且在完成它们之后总是正确地减少引用计数(这将在运行代码后释放这些变量)。

On a side note - this is a common issue. 在旁注 - 这是一个常见问题。 Many other packages, such as numpy, or any package written using Boost::Python will exhibit the same behavior if you use Py_Finalize . 如果使用Py_Finalize ,许多其他包(如numpy或使用Boost :: Python编写的任何包)都会表现出相同的行为。

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

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