Jump to content
Celebr0

Each .py script in a separate window

Recommended Posts

Hello, please tell me, I need to run one .py script, but every time in a new window, and by default it runs in one:

 

procedure ExecPythonFile(const MyPyFile:String);
var
Py: IPyEngineAndGIL;
begin
Py := SafePyEngine;
try
Py.PythonEngine.ExecFile(MyPyFile);
except
end;
end;

Due to the fact that the script runs in only one window, my small program crashes even on 10 threads =(
 

Share this post


Link to post
Posted (edited)

Perhaps I asked the question a little incorrectly, here's how I do it:
 

type
TPyThread = class(TPythonThread)
  protected
  procedure ExecuteWithPython; override;
  public
  constructor Сreate(createsuspended: boolean);
end;

procedure ExecPythonFile(const FFile, Params:string);
const
Arg: string =
'import sys' + #10 +
'def run_python_script(args_str):'+#10+
'    args_list = args_str.split()'+#10+
'    sys.argv = [sys.argv[0]] + args_list';
var
Py: IPyEngineAndGIL;
begin
try
PythonEngine.ExecString(Arg+#10+'run_python_script("'+Params+'")');
PythonEngine.ExecFile(FFile);
finally
end;
end;

constructor TPyThread.Сreate(createsuspended: boolean);
begin
inherited Create(CreateSuspended);
ThreadExecMode:=emNewInterpreterOwnGIL; // I USE Python12.dll
FreeOnTerminate:=True;
end;

procedure TPyThread.ExecuteWithPython;
while true do
ExecPythonFile(myfilename,'-i 2000 --r');
end;

My software just crashes after a while, please help 

In one thread it works fine, but at 10 the software crashes

Edited by Celebr0

Share this post


Link to post

I don't have an answer but you code is so hard to read because wrong indentation that some will not bother to read you code and answer.

I understand that Pascal syntax is permissive but do you write your code in Python also with wrong indentation? And it works?

Share this post


Link to post
2 hours ago, Cristian Peța said:

I don't have an answer but you code is so hard to read because wrong indentation that some will not bother to read you code and answer.

I understand that Pascal syntax is permissive but do you write your code in Python also with wrong indentation? And it works?

Dude, all the indentations are correct everywhere, I don’t know where you got the idea that they are incorrect?

Share this post


Link to post
Posted (edited)

Dude, although its not so much code you posted this is more readable:

 

type

TPyThread = class(TPythonThread)
  protected
    procedure ExecuteWithPython; override;
  public
    constructor Сreate(createsuspended: boolean);
end;

procedure ExecPythonFile(const FFile, Params:string);
const Arg: string = 
	'import sys' + #10 +
	'def run_python_script(args_str):'+#10+
	'    args_list = args_str.split()'+#10+
	'    sys.argv = [sys.argv[0]] + args_list';
var Py: IPyEngineAndGIL;
begin
  try
    PythonEngine.ExecString(Arg+#10+'run_python_script("'+Params+'")');
    PythonEngine.ExecFile(FFile);
  except
    Raise;
  end;
end;

constructor TPyThread.Сreate(createsuspended: boolean);
begin
  inherited Create(CreateSuspended);
  ThreadExecMode:=emNewInterpreterOwnGIL; // I USE Python12.dll
  FreeOnTerminate:=True;
end;

procedure TPyThread.ExecuteWithPython;
Begin
  while true do
    ExecPythonFile(myfilename,'-i 2000 --r');
end;

 

Edited by Die Holländer

Share this post


Link to post
1 hour ago, Celebr0 said:

Dude, all the indentations are correct everywhere, I don’t know where you got the idea that they are incorrect?

Yes, syntactically they are correct but I don't bother to read a code without indentations.

Share this post


Link to post
Posted (edited)

Does my code look exactly like this or differently?

 

 

 


 

2024-07-24_013620.thumb.jpg.47eaf2bd4a8625f080cf17c8d796f5cd.jpg

 
 

 

Edited by Celebr0

Share this post


Link to post

Hello, look what I need to achieve, I can run one .py script in at least 10 console windows at the same time, but I can’t achieve this through Python4Delphi with GIL locking only one running .py script works at a time

Share this post


Link to post
Posted (edited)

When you start 10 console windows you are starting 10 processes.

With Python4Delphi you will start all 10 in the same process. In this way you are limited by GIL.

You can start from a Delphi app 10 process executing same Python script but not using Python4Delphi.

Python4Delphi runs in your process and so you have a lot of advantages but also GIL.

 

PS. You can start at the same time 10 Delphi app and you will have 10 Python scripts running from Python4Delphi at the same time without GIL limitation.

Edited by Cristian Peța

Share this post


Link to post
On 7/30/2024 at 1:11 PM, Cristian Peța said:

When you start 10 console windows you are starting 10 processes.

With Python4Delphi you will start all 10 in the same process. In this way you are limited by GIL.

You can start from a Delphi app 10 process executing same Python script but not using Python4Delphi.

Python4Delphi runs in your process and so you have a lot of advantages but also GIL.

 

PS. You can start at the same time 10 Delphi app and you will have 10 Python scripts running from Python4Delphi at the same time without GIL limitation.

I just can't run 10 instances of a .py script through Python4Delphi, right? - There must be a bad low-quality library (

Share this post


Link to post
Posted (edited)
8 hours ago, Celebr0 said:

I just can't run 10 instances of a .py script through Python4Delphi, right?

You can run 10 instances with Python4Delphi but you have GIL limitation because Python. Python limits you, not Python4Delphi. Is there a library in this word that can circumvent GIL? 

You can use CreateProcess and run 10 processes. You don't need Python4Delphi for this. But in the same processes you are limited by GIL. And all this because Python, not Python4Delphi.

Is Python a bad and low quality implementation of the language because this?

Edited by Cristian Peța

Share this post


Link to post
6 hours ago, Cristian Peța said:

You can run 10 instances with Python4Delphi but you have GIL limitation because Python. Python limits you, not Python4Delphi. Is there a library in this word that can circumvent GIL? 

You can use CreateProcess and run 10 processes. You don't need Python4Delphi for this. But in the same processes you are limited by GIL. And all this because Python, not Python4Delphi.

Is Python a bad and low quality implementation of the language because this?

Hello again, in new versions of Python, they accelerated the GIL in python4delphi, is it emNewInterpreterOwnGIL? I just noticed that emNewInterpreterOwnGIL does not work with .py files ?

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

×