Jump to content
Sign in to follow this  
tomye

if i changed the P4D DLLPath how to reload DLLs ?

Recommended Posts

q1.png.677f4b2b11c4ae8893a13a3239c47f10.png

 

if write like this , got an error :  There is already one instance of TPythonEngine running

 

the P4D can not change Python DLLs in runtime ?

 

Share this post


Link to post
7 hours ago, pyscripter said:

Set AutoLoad to False and call LoadDLL at Runtime (e.g. in your FormCreate).

Yes, i did , but does not work , so i post the question on here

 

does the P4D not support LOADDLL and UNLOADDLL dynamic call ? 

 

for example:

 

i launch the exe and call the LoadDLL function at first time

 

after run one script i need  reset a new DLL path and call LoadDLL again, 

 

how to do like this? 

Share this post


Link to post
18 hours ago, tomye said:

Yes, i did , but does not work , so i post the question on here

 

does the P4D not support LOADDLL and UNLOADDLL dynamic call ? 

It does, but it is not a good idea and should be avoided.  See demo34 for how it can work.  This demonstrates also AutoLoad = False

Share this post


Link to post
1 hour ago, pyscripter said:

It does, but it is not a good idea and should be avoided.  See demo34 for how it can work.  This demonstrates also AutoLoad = False

Has this changed? I remember failing to do this, unload and reload the dll in my own wrapper of embedded Python. But that was very many years ago so things likely have changed. 

Share this post


Link to post
8 hours ago, David Heffernan said:

Has this changed?

Unloading and reloading has always been possible with P4D, but quite tricky and not foolproof.  You need to manually unload the dll's loaded by imported modules and if you fail to do that it will fail.   PyScripter does that and allows you to change python version without exiting.

  • Like 1

Share this post


Link to post
1 hour ago, David Heffernan said:

How do you find out which DLLs to unload? 

PyScripter is opensource so you can read the code. A quick look seems to suggest it doesn't - it unloads a bunch of common .pyd that might be loaded. I could be missing something however.

In file cInternalPython.pas  the procedure TInternalPython.DestroyPythonComponents;  from https://github.com/pyscripter/pyscripter

  • Thanks 1

Share this post


Link to post
19 hours ago, Brian Evans said:

PyScripter is opensource so you can read the code. A quick look seems to suggest it doesn't - it unloads a bunch of common .pyd that might be loaded.

Indeed.  As I said it is not foolproof.  If you use a specific set of python modules, you can see in Delphi's debug mode which DLLs are loaded when your python scripts execute.  You can then write code to unload them after unloading python.   Failing to unload a DLL does not necessarily mean that there will be an issue next time you load python.   

Share this post


Link to post

Thanks, this helps. None of this would be necessary if python modules unloaded their DLLs so I'm guessing that Python itself doesn't provide hooks to modules to let them do that. Or perhaps is it just that modules aren't in the habit of unloading their DLLs. I think it would be easy to do for extension modules. But I'm not sure how a Python module that loaded DLLs with ctypes would get a hook to unload them.

 

Certainly it sounds like what OP is trying to do is not very viable. Especially if the Python code that is executed can be specified at runtime. 

Share this post


Link to post
14 hours ago, David Heffernan said:

But I'm not sure how a Python module that loaded DLLs with ctypes would get a hook to unload them

DllMain with DLL_PROCESS_DETACH parameter?

Share this post


Link to post
26 minutes ago, Fr0sT.Brutal said:

DllMain with DLL_PROCESS_DETACH parameter?

For a Python extension module. But a pure Python module isn't a DLL. 

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
Sign in to follow this  

×