Jump to content
Sign in to follow this  
Celebr0

Am I doing something wrong ?

Recommended Posts

Hello, I think I checked everything 7 times, I seem to be doing everything correctly according to the code, but I get an error:

 

Subinterpreter with own GIL:
Traceback (most recent call last):
  File "<string>", line 1, in <module>
NameError: name 'HW' is not defined

 

My Code:

 

program ParallelPython;

{$APPTYPE CONSOLE}

uses
  System.SysUtils,
  System.Diagnostics,
  System.Variants,
  System.SyncObjs,
  PythonEngine;

var
  PythonEngine: TPythonEngine;

procedure CreatePyEngine;
begin
  PythonEngine := TPythonEngine.Create(nil);
  PythonEngine.InitScript.Text:='HW="Hello World!"';
  PythonEngine.Name := 'PythonEngine';
  PythonEngine.IO:=nil;
  PythonEngine.RedirectIO:=False;
  PythonEngine.LoadDll;
  TPythonThread.Py_Begin_Allow_Threads;
end;

procedure DestroyEngine;
begin
  TPythonThread.Py_End_Allow_Threads;
  PythonEngine.Free;
end;

type
  TPyThread = class(TPythonThread)
  protected
    procedure ExecuteWithPython; override;
  public
    constructor Create(ThreadMode: TThreadExecMode);
  end;

procedure TPyThread.ExecuteWithPython;
begin
  writeln(PythonEngine.EvalStringAsStr('HW'));
end;

constructor TPyThread.Create(ThreadMode: TThreadExecMode);
begin
  inherited Create;
  ThreadExecMode := ThreadMode;
  FreeOnTerminate := True;
end;

var
  I: Integer;
begin
  try
    CreatePyEngine;
    try
      WriteLn('Subinterpreter with own GIL:');
        TPyThread.Create(emNewInterpreterOwnGIL);
    finally
      //Sleep(10);  // allow some time for the threads to terminate
      //DestroyEngine;
    end;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
  ReadLn;
end.

 

Share this post


Link to post

The variable HW is defined in the main interpreter and is not available in the newly created sub-interpreter, which is isolated from the main interpreter.  Hence the error.

 

PS: I wonder why everyone is so obsessed with running python in threads, especially GIL owning ones.  This is quite an advanced feature and requires deep understanding of the topic.   P4D makes it easy to use this feature, but this does not mean that users do not need to study the python documentation and understand the nuances.  To use an analogy, Delphi encapsulates threads pretty well but doing thread programming in Delphi is not trivial and requires understanding the pitfalls.

Edited by pyscripter

Share this post


Link to post
2 hours ago, pyscripter said:

The variable HW is defined in the main interpreter and is not available in the newly created sub-interpreter, which is isolated from the main interpreter.  Hence the error.

 

PS: I wonder why everyone is so obsessed with running python in threads, especially GIL owning ones.  This is quite an advanced feature and requires deep understanding of the topic.   P4D makes it easy to use this feature, but this does not mean that users do not need to study the python documentation and understand the nuances.  To use an analogy, Delphi encapsulates threads pretty well but doing thread programming in Delphi is not trivial and requires understanding the pitfalls.

Hello, the thing is that Python has something that Delphi does not have and will never have, for example, Tensorflow AI and so on.

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  

×