Jump to content
Sign in to follow this  
pyscripter

Running python code in Delphi threads

Recommended Posts

There have been a lot of questions in this forum about running python code in threads using Python4Delphi.   I have created a comprehensive guide in this Wiki topic.

  • Like 3

Share this post


Link to post

Is a 2006 Programming Python by Mark Lutz still worth reading? Then use your guide to learn how use threads with Python.  Thanks      

Share this post


Link to post

My recollection of Lutz's work is that Learning Python is the book of his to read.

 

As for the Python 2 vs 3 distinction, I'd be less worried about that. That's more about syntax details than concepts. A really good work written for Python 2 is better than an average work for Python 3. The differences between 2 and 3 aren't significant conceptually. 

  • Like 1

Share this post


Link to post

It seems ThreadPythonExec does not work. and memory leak occurs

 

procedure TForm1.Button2Click(Sender: TObject);
begin
  ThreadPythonExec(
    procedure
    begin
      GetPythonEngine.Execstring('print(10)');
    end);
end;

Leak.png

Share this post


Link to post

@maomao2028 Works fine here without memory leaks.   See attached project.

 

Note the use of DelayWrites := True in the PythonGUIInputOutput and the calls to TPythonThread.Py_End_Allow_Threads and TPythonThread.Py_Begin_Allow_Threads.

Demo01.zip

Share this post


Link to post
On 4/22/2024 at 6:56 PM, pyscripter said:

@maomao2028 Works fine here without memory leaks.   See attached project.

 

Note the use of DelayWrites := True in the PythonGUIInputOutput and the calls to TPythonThread.Py_End_Allow_Threads and TPythonThread.Py_Begin_Allow_Threads.

Demo01.zip

Thanks for your help, now the thread is running fine

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
Sign in to follow this  

×