Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Demo34 + NumPy #287

Closed
dominiced opened this issue Feb 23, 2021 · 1 comment
Closed

Demo34 + NumPy #287

dominiced opened this issue Feb 23, 2021 · 1 comment
Labels

Comments

@dominiced
Copy link

dominiced commented Feb 23, 2021

Hello!

I would really appreciate some help. I want to completely clear Python state after each script execution so a fresh interpreter is ready for the next time a script is executed. I've used Demo34 as an example for completely destroying and recreating an engine for each script execution, however, this causes problems with NumPy because NumPy doesn't like being finalized and then reinitialized.

This simple problem can be recreated by running demo34, modifying the Python script to simply "import numpy" and nothing else, execute, and then switch to and from a different python version to cause it to destroy and recreate the Python engine.

image

  1. Since we're completely recreating the Python engine, I would have expected all previous memory and state to be cleared. How come NumPy knows it's been previously run?
  2. Is there a better way of completely clearing state when re-running Python scripts in P4D? One which might not cause issues for NumPy? I need to completely clear state, because otherwise I get other issues because old variables are hanging around in memory. I'd prefer everything to be cleaned up after each run.

Thank you.

@pyscripter
Copy link
Owner

  • Please ask questions at the P4D support forum https://en.delphipraxis.net/?forumId=39
  • Recreating the engine is in general not a good idea and not recommended or supported
  • You need to manually unload any dlls loaded by your python script before recreating the engine

For example PyScripter does something like this

  procedure UnloadPythonDLL(DLL: PWideChar);
  Var
    Module : HMODULE;
  begin
    MODULE := GetModuleHandle(DLL);
    if MODULE >= 32 then
      FreeLibrary(Module);
  end;

  if WasLoaded then
  begin
    UnloadPythonDLL('select.pyd');
    UnloadPythonDLL('_socket.pyd');
    UnloadPythonDLL('python3.dll');
    UnloadPythonDLL('_ssl.pyd');
    UnloadPythonDLL('win32file.pyd');
    UnloadPythonDLL('win32pipe.pyd');
    UnloadPythonDLL('win32event.pyd');
    UnloadPythonDLL('_win32sysloader.pyd');
    UnloadPythonDLL('unicodedata.pyd');
    UnloadPythonDLL('_ctypes.pyd');
    UnloadPythonDLL('_hashlib.pyd');
    UnloadPythonDLL('_asyncio.pyd');
    UnloadPythonDLL('_overlapped.pyd');
    UnloadPythonDLL('_bz2.pyd');
    UnloadPythonDLL('_lzma.pyd');
    UnloadPythonDLL('_queue.pyd');
    UnloadPythonDLL(PChar('pywintypes' + RegVersion + '.dll'));
  end;

But you need to use the debugger to see what dlls your script loads.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants