简体   繁体   English

Python:关闭父应用程序后,如何(不)在 memory 中保持 COM 对象持久化?

[英]Python: How to (not) keep a COM-object persistent in memory after closing the parent application?

i am working on a (pretty big) Python/Tkinter-application (Python 2.7 under Windows 7) which (among many other things) calls Matlab via the COM-interface.我正在开发一个(相当大的)Python/Tkinter 应用程序(Windows 7 下的 Python 2.7),它(除其他外)通过 COM 接口调用 Matlab。 The basic structure of the Matlab/COM-part is like this: Matlab/COM 部分的基本结构是这样的:

import Tkinter
import pythoncom
import win32com.client

class App( object ):

    def __init__( self, parent ):
        Tkinter.Button( root, text="Start Matlab", command=self.start_matlab ).grid()

    def start_matlab( self ):
        self.matlab = win32com.client.Dispatch( "Matlab.Application" )

root = Tkinter.Tk()
App( root )
root.mainloop()

The behaviour i observe with this simplified code is: Running the application and clicking the button creates a Matlab-Instance (opens a Matlab-Command window), and when closing the Tkinter application also that Matlab-window and the corresponding entry in the Task-Manager disappear.我用这个简化的代码观察到的行为是:运行应用程序并单击按钮创建一个 Matlab 实例(打开一个 Matlab 命令窗口),并且在关闭 Tkinter 应用程序时,该 Matlab 窗口和任务中的相应条目 -经理消失。 Repeating the procedure, Matlab is started afresh.重复该过程,重新启动 Matlab。 However, when i do "the same" with my "real" application, the Matlab instance persists after closing my application and, moreover, when i restart the application and run the part which "starts" Matlab, it just retrieves and uses the instance which remained in memory after quitting the first sessiong of my app.但是,当我对“真实”应用程序执行“相同”操作时,Matlab 实例在关闭我的应用程序后仍然存在,此外,当我重新启动应用程序并运行“启动”Matlab 的部分时,它只是检索并使用该实例退出我的应用程序的第一个 sessiong 后,它仍保留在 memory 中。 Unfortunately, i am not able to isolate a reasonably small code example showing the latter behavior:(不幸的是,我无法隔离显示后一种行为的相当小的代码示例:(

Does anybody have an idea what the reason for this is/could be?有谁知道这是/可能是什么原因?

How can one control whether a COM-object is killed or persists in memory when the parent Python application which created it is closed?当创建它的父 Python 应用程序关闭时,如何控制 COM 对象是被杀死还是保留在 memory 中?

Here's how to explicitly remove the COM object, using Tkinter's protocol handler:以下是使用 Tkinter 的协议处理程序显式删除 COM object 的方法:

import Tkinter
import pythoncom
import win32com.client

class App( object ):

    def __init__( self, parent ):
        self.parent = parent #reference to root
        Tkinter.Button( root, text="Start Matlab", command=self.start_matlab ).grid()
        self.parent.protocol('WM_DELETE_WINDOW', self.closeAll) #protocol method

    def start_matlab( self ):
        self.matlab = win32com.client.Dispatch( "Matlab.Application" )

    def closeAll(self):
        del self.matlab       #delete the COM object
        self.parent.destroy() #close the window

root = Tkinter.Tk()
App( root )
root.mainloop()

Reference: Removing COM object from memory参考: 从 memory 中移除 COM object

More on protocols 更多关于协议

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

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