pcoenen 1 Posted 9 hours ago I'm using python 4 Delphi and TPythonEngine in emNewInterpreterOwnGIL ThreadExecMode. In this mode I can not use the global IO property of TPythonEngine. Still, I would like to receive output (unicode) information from the Engine. I'm using multiple TPythonhreads which are started from different Delphi TThreads (One TThread starts one TPythonThread). Each thread should get its own unique infromation because of the emNewInterpreterOwnGIL. At the moment I have added 'import sys; from io import StringIO; __buf = StringIO(); sys.stdout = __buf; sys.stderr = __buf' to the Python script and after ExecStrings I use EvalStringAsStr('__buf.getvalue()') to get the output information. It seems to be working but is this the correct way or any better ideas out there? Thank you, Pascal Share this post Link to post
pyscripter 776 Posted 5 hours ago (edited) For now, there are very serious limitations you need to consider when using emNewInterpreterOwnGIL to achieve true parallelism. See for instance the limitations in What’s new in Python 3.14 — Python 3.14.0b3 documentation. Even the innocent looking print command is not thread-safe. And almost nothing can be share between the interpreters. Also there are limitations about what you can import, and P4D modules are not yet considered safe for use with emNewInterpreterOwnGIL. This is changing with the forthcoming python 3.14. It will include a new module concurrent.interpreters that exposes the interpreters with their own GIL to pure python code. There will also be an addition of ways to communicate between the interpreters using queues. See PEP 734. So for example you could store your output to such a queue and print it when everything is finished. But all the above is cutting edge and unless you really need it you should avoid it, If you decide to use it then make sure you fully understand the limitations and implications. I will try to make P4D modules compatible with emNewInterpreterOwnGIL and that would at least give you the option to say add output to a Delphi string list (or something similar) protected with a global lock on the Delphi side. Note that all the above are not related to the free-threading version of Python, which in itself is another story altogether. Edited 5 hours ago by pyscripter 1 Share this post Link to post