Celebr0 0 Posted July 20 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
Celebr0 0 Posted July 22 (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 July 22 by Celebr0 Share this post Link to post
Cristian Peța 107 Posted July 23 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
Celebr0 0 Posted July 23 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
Die Holländer 49 Posted July 23 (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 July 23 by Die Holländer Share this post Link to post
Cristian Peța 107 Posted July 23 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
JonRobertson 72 Posted July 23 2 hours ago, Celebr0 said: Dude, all the indentations are correct everywhere, I don’t know where you got the idea that they are incorrect? Because only the TPyThread type definition was indented. https://en.delphipraxis.net/topic/11903-each-py-script-in-a-separate-window/?do=findComment&comment=93799 Share this post Link to post
Celebr0 0 Posted July 23 (edited) Does my code look exactly like this or differently? Edited July 23 by Celebr0 Share this post Link to post
Celebr0 0 Posted July 26 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
Cristian Peța 107 Posted July 30 (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 July 30 by Cristian Peța Share this post Link to post
Celebr0 0 Posted August 5 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
Cristian Peța 107 Posted August 6 (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 August 6 by Cristian Peța Share this post Link to post
Celebr0 0 Posted August 6 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
Cristian Peța 107 Posted August 6 (edited) About emNewInterpreterOwnGIL. Not every script can run with this. There are limitations. https://github.com/pyscripter/python4delphi/discussions/442 Edited August 6 by Cristian Peța Share this post Link to post