Py_Initialize和Py_finalize和MatPlotlib [英] Py_Initialize and Py_finalize and MatPlotlib

查看:188
本文介绍了Py_Initialize和Py_finalize和MatPlotlib的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个已知的问题,但是我想请专家为我解决问题的最佳方法.

This is a known problem, but I want to ask the experts for the best way to solve it for me.

我有一个项目(Euler Math Toolbox),该项目将Python作为脚本语言运行.为此,在运行时加载库模块"python.dll",该模块与"python27.lib"链接.然后调用Py_Initialize.这一切都很好.

I have a project (Euler Math Toolbox), which runs Python as a script language. For this, a library module "python.dll" is loaded at run time, which is linked against "python27.lib". Then Py_Initialize is called. This all works well.

但是用户可以使用新的会话和笔记本重新启动Euler.然后,我希望Python清除所有变量和导入.为此,我调用Py_Finalize并卸载"python.dll".当需要Python时,加载和初始化将再次启动Python.

But Euler can be restarted by the user with a new session and notebook. Then I want Python to clear all variables and imports. For this, I call Py_Finalize and unload "python.dll". When Python is needed, loading and initializing starts Python again.

这有效.但是,如果在上一个会话中导入了MatPlotlib,则Python会在第一次调用时崩溃.似乎Py_Finalize不能完全清除Python,也不能卸载我的"python.dll".我尝试卸载"python27.dll"(Python DLL),但这无济于事.最有可能的是,另一个DLL保持活动状态,但是在Py_Finalize期间损坏了.

This works. But Python crashes at the first call, if MatPlotlib is imported in the previous session. It seems that Py_Finalize does not completely clear Python, nor does unloading my "python.dll". I tried unloading "python27.dll" (the Python DLL), but this does not help. Most likey, another DLL remains active, but corrupts during Py_Finalize.

要解决此问题,只需清除所有变量和导入即可.我可以忍受不打电话给Py_Finalize.但是如何?

To solve this, it would suffice to clear all variables and imports. I could live with not calling Py_Finalize. But how?

PS:您可能想知道,为什么我不直接将euler.exe链接到Python.原因是,即使没有安装Python,这也阻止了Euler表单的启动.

PS: You may wonder, why I do not directly link euler.exe to Python. The reason is that this prevents Euler form starting, if Python is not installed, even if it is never needed.

谢谢您的回答!如果愿意,您可以将重复的答案复制给gmail的renegrothmann.那对我有帮助.

Thanks for any answers! You duplicate your answer to renegrothmann at gmail, if you like. That would help me.

推荐答案

要解决此问题,只需清除所有变量和导入即可.我可以忍受不打电话给Py_Finalize.但是如何?

To solve this, it would suffice to clear all variables and imports. I could live with not calling Py_Finalize. But how?

假设您在每次调用后都正确释放了所有引用,这应该可以正常工作.只要确保只调用一次Py_Initialize,就不要调用Py_Finalize.使用单独的词典运行每个会话",并在使用完它们后始终正确减少引用计数(在运行代码后将释放这些变量).

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. 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).

附带说明-这是一个常见问题.如果您使用Py_Finalize,许多其他软件包(例如numpy或使用Boost :: Python编写的任何软件包)将表现出相同的行为.

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_Initialize和Py_finalize和MatPlotlib的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆