Jump to content
CBAPKA

How I can clear engine after exec script ?

Recommended Posts

procedure Thread.ExecuteWithPython;
begin
  inherited;

  var Lines: TStringlist := TStringlist.Create;
  Lines.Text := code;
  var ppO: PPyObject;
  try
    ppO := GetPythonEngine.Run_CommandAsObject(Lines.Text, file_input, '<string>');
  finally
    FreeAndNil(Lines);
    GetPythonEngine.Py_DECREF(ppO);
  end;


end;

Okay, I has new problem, I tried understand this, but cant. Oken I can exec script as example

 

a = 10

Script was executed

 

procedure TMainForm.TerminateThread(Sender: Tobject);
var
  th: Thread absolute Sender;
begin
  work := false;
  TPythonThread.Py_End_Allow_Threads;
  ShowMessage('ready!');
end;

and I launch againt but code python now is:

 

print(a)

And he knew about this variable, it shouldn't be like this... I have a Telegram database that is blocked after initializing the TelegramClient variable. I need to free up memory after executing the code, because I have many different pieces of Python code that are executed by pressing different buttons, but the engine is the same, so it needs to be cleaned up somehow. I tried to Google the information, but it's difficult when you don't understand much, and even more difficult when there is very little information)

Share this post


Link to post
Posted (edited)

Hint:  The answer is in PythonThreads · pyscripter/python4delphi Wiki (github.com)

 

Other solutions include:

  • Modify your scripts so that they do not create global variables (wrap everything inside a function which you execute)
  • At the end of your python script delete (del) any global variables you create
  • Use Run_CommandAsObjectWithDict or ExecStrings, passing new empty dictionaries to the optional locals, and globals parameters, which you then destroy.

 

Edited by pyscripter

Share this post


Link to post
Posted (edited)
7 hours ago, pyscripter said:

Hint:  The answer is in PythonThreads · pyscripter/python4delphi Wiki (github.com)

 

Other solutions include:

  • Modify your scripts so that they do not create global variables (wrap everything inside a function which you execute)
  • At the end of your python script delete (del) any global variables you create
  • Use Run_CommandAsObjectWithDict or ExecStrings, passing new empty dictionaries to the optional locals, and globals parameters, which you then destroy.

 

Thank you, I've managed to solve some of my issues, but the best solution - to remove these variables from Python.

procedure Thread.ExecuteWithPython;
begin
  inherited;
  with GetPythonEngine do
    Run_CommandAsObjectWithDict(code, file_input, PyDict_New, PyDict_New);
end;

 

Edited by CBAPKA

Share this post


Link to post
Posted (edited)
1 hour ago, CBAPKA said:

PyDict_New, PyDict_New

You need to decrease the refcount of these pyobjects after using them.

Edited by pyscripter

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

×