Skip to content

Demo34 + NumPy #287

Closed
Closed

Description

@dominiced

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.

Activity

  1. pyscripter commented on Feb 23, 2021

    @pyscripter
    Collaborator
    • 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions