pyscripter / python4delphi

Free components that wrap up Python into Delphi and Lazarus (FPC)
MIT License
906 stars 310 forks source link

Why is multithread not working? #406

Closed Uefi1 closed 1 year ago

Uefi1 commented 1 year ago

Hello, why is this code not working, causing access violation errors and others for example already running one instance of TPythonEngine. ?

program Project1;

{$APPTYPE CONSOLE}

uses
  SysUtils,
  Classes,
  System.syncobjs,
  PythonEngine;

type
th = class(TPythonThread)
private
procedure OnAfterInit(Sender:Tobject);
procedure OnBeforeUnload(Sender:Tobject);
protected
  procedure Execute; Override;
public
  constructor create(createsuspended:boolean);
end;

var
cs:TcriticalSection;

{ th }

constructor th.create(createsuspended: boolean);
begin
inherited Create(CreateSuspended);
ThreadExecMode:=emNewInterpreter;
FreeOnTerminate:=True;
end;

procedure th.OnAfterInit(Sender:Tobject);
begin
Py_Begin_Allow_Threads;
end;

procedure th.OnBeforeUnload(Sender: Tobject);
begin
Py_End_Allow_Threads;
end;

procedure th.Execute;
var
val:string;
OwnThreadState: PPyThreadState;
PythonEngine1:TPythonEngine;
fThreadState: PPyThreadState;
fGILState: PyGILstate_STATE;
begin
inherited;
while true do begin
  PythonEngine1:=TPythonEngine.Create(nil);
  PythonEngine1.InitThreads:=True;
  PythonEngine1.AutoLoad:= False;
  PythonEngine1.AutoFinalize:=False;
  PythonEngine1.AutoUnload:=False;
  PythonEngine1.RedirectIO:=True;
  PythonEngine1.OnAfterInit:=OnAfterInit;
  PythonEngine1.OnBeforeUnload:=OnBeforeUnload;
  PythonEngine1.LoadDll;
  PythonEngine1.ExecString('fun=("Hello World!")');
  val:=(PythonEngine1.EvalStringAsStr('fun'));
  PythonEngine1.Py_Finalize;
  PythonEngine1.UnloadDll;
  FreeAndNil(PythonEngine1); // or .Free
cs.Enter;
Write(Format(#13'VALUE=%s %d',[val,Random(50000)]));
cs.Leave;
if not Terminated then
Terminate;
DoTerminate;
end;
end;

var
i:integer;
begin
//ReportMemoryLeaksOnShutdown:=True;
Randomize;
cs:=TcriticalSection.Create;
for i:=0 to 1 do
th.create(false);
readln;
end.
pyscripter commented 1 year ago

"Why my code does not work" is not a bug report. Please use the support forum.

Uefi1 commented 1 year ago

"Why my code does not work" is not a bug report. Please use the support forum. I was banned on the support forum, so I'm waiting for support exactly in place from you, the developer, whatever I did, I can't work in threads in Python4Delphi

dbcto commented 1 year ago

Maybe you should ask yourself why you were banned? Maybe because it's the way you demand something here without having any claim to it. We should be glad that there are people who keep the open source community going - especially in the area of Delphi. I think a little respect would be appropriate here. So please question yourself and make your contribution to this project. As it looks, you are using it.

Uefi1 commented 1 year ago

Maybe you should ask yourself why you were banned? Maybe because it's the way you demand something here without having any claim to it. We should be glad that there are people who keep the open source community going - especially in the area of Delphi. I think a little respect would be appropriate here. So please question yourself and make your contribution to this project. As it looks, you are using it. No, that forum delphipraxis is just stupid morons