Jump to content
softtouch

Freeing TPythonModule after using it takes long

Recommended Posts

I create a TPythonModule, and add a delphi methode in the OnInitialization event of the module with TPythonModule(Sender).AddDelphiMethod('....') to it.

If I dont call the method from Python, module.free is instantly, but once I have called this delphi method, even the method does noting, it takes a couple of seconds for module.free to return.

Is there something I need to do to prevent this delay?

Share this post


Link to post
Posted (edited)

You are not supposed to destroy TPythonModules before engine destruction.  They are automatically destroyed when the engine is destroyed.  And it does not achieve the desired effect of removing it from python.

 

Python does not have a good mechanism for removing imported modules and this is not recommended in general.  See for instance How do I really delete an imported module? (archive.org).  Also from Reloading modules in Python - GeeksforGeeks 

 

Quote

 

How do I unload a Python module?

Unloading a Python module is not possible in Python yet.

 

Not sure what causes the delay.   

 

 

Edited by pyscripter

Share this post


Link to post

I stopped the time and it takes around 5 second until the .free returns. If I dont free it and just free the pythonengine, that will take 5 seconds (I guess for releasing the module). It definitely only take so long when the delphi function was called once from python, even the delphi function does not do anything. If I dont call the delphi function from python, there is no delay at all.

Share this post


Link to post
Posted (edited)

Destroying the engine and associated modules is normally instantaneous.  Most of the demos do that.  So that delay should be related to something "special" you are doing.  Is python code running while you are doing that?

As an aside, why do you need to manually free the engine? 

Edited by pyscripter

Share this post


Link to post

A minimum example, which works just fine but cause the delay, and I cant figure out why that is.

 

In form create:

procedure TfrmTest.FormCreate(Sender: TObject);
begin
  eng:=TPythonEngine.Create(nil);
  eng.DllName:='python312.dll';
  eng.DllPath:=pythonhome;
  module:=TPythonModule.Create(nil);
  module.Engine:=eng;
  module.ModuleName:='delphicallback';
  module.OnInitialization:=InitPythonModule;
  eng.LoadDll;
end;


In form close:

procedure TfrmTest.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  eng.Free;    // Takes 5 seconds to return once the python script called the delphi method
end;


The InitPythonModule has just this one line:

procedure TfrmTest.InitPythonModule(Sender: TObject);
begin
  TPythonModule(Sender).AddDelphiMethod('callback',delphi_callback,'callback(name, value)->value');
end;


The delphi method "delphi_callback" only does this for testing:

function TfrmTest.delphi_callback(pself, args: PPyObject): PPyObject;
var
  key:pansichar;
  value:PPyObject;
begin
  with GetPythonEngine do
  begin
    if PyArg_ParseTuple(args, 'sO:callback', @key, @value) <> 0 then
    begin
      result:=PyUnicodeFromString('Test');
    end;
  end;
end;


Calling the python script

procedure Tfrmtest.btntestClick(Sender: TObject);
begin
  eng.ExecFile(<the script file path here>);
  s:=MainModule.Test('Hello world');
end;


The python script does this for testing:

import delphicallback

def Test(s:str)->str:
  delphicallback.callback('testing',s)

 

Share this post


Link to post

Could you post your minimal Delphi project in a zip file? 

Share this post


Link to post

Sorry for the late replay. I believe I found the issue, its related to selenium somehow. Once I use selenium, freeing the module takes 5 seconds, even the selenium driver was already closed earlier.

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×