Пожалуйста, обратите внимание, что пользователь заблокирован
Здрасте, удалось ли кому то победить библиотеку Python4Delphi у меня она напрочь отказывается работать в многопотоке:
В 1-2 потока иногда работает и то праздникам
Код:
{$APPTYPE CONSOLE}
uses
SysUtils,
Classes,
System.syncobjs,
PythonEngine;
type
th = class(TPythonThread)
private
protected
procedure Execute; Override;
public
constructor Create(CreateSuspended: Boolean);
end;
var
cs: TCriticalSection;
PythonEngine:TPythonEngine;
{ th }
constructor th.Create(CreateSuspended: Boolean);
begin
inherited Create(CreateSuspended);
ThreadExecMode := emNewInterpreter;
FreeOnTerminate := True;
end;
procedure th.Execute;
var
scr,val: string;
PyFunc: PPyObject;
begin
inherited;
while True do
begin
PythonEngine.ExecString('HW="Hello World!";');
cs.Enter;
Writeln(PythonEngine.EvalStringAsStr('HW')); // Вывод значения в консоль
cs.Leave;
//pyfunc:=PythonEngine.FindFunction(PythonEngine.ExecModule,'');
//PythonEngine.Py_DecRef(PyFunc);
end;
//PythonEngine.PyEval_AcquireThread(Self.ThreadState);
//PythonEngine.PyErr_SetString(PythonEngine.PyExc_KeyboardInterrupt^, 'Terminated');
//PythonEngine.PyEval_ReleaseThread(Self.ThreadState);
end;
var
i: Integer;
begin
PythonEngine:=TPythonEngine.Create(nil);
PythonEngine.InitThreads := True;
PythonEngine.DllPath := ExtractFilePath(ParamStr(0)) + '\python';
PythonEngine.DllName := 'python310.dll';
PythonEngine.UseLastKnownVersion := False;
PythonEngine.AutoLoad := False;
PythonEngine.AutoFinalize := True;
PythonEngine.AutoUnload := False;
PythonEngine.RedirectIO := False;
PythonEngine.LoadDll;
Randomize;
cs := TCriticalSection.Create;
for i := 1 to 4 do
th.Create(False);
readln;
end.
В 1-2 потока иногда работает и то праздникам