代码之家  ›  专栏  ›  技术社区  ›  Alexander McFarlane

在Python 2.7中安全卸载/重载C++ DLL(用新版本替换DLL)

  •  0
  • Alexander McFarlane  · 技术社区  · 8 年前

    我问这个问题的背景是关于这个话题的大多数其他问题都是5-10岁,并给出了以下内容

    • Windows 7操作系统
    • 多个版本的 .pyd 在不同文件路径位置编译的dll,例如。
      • /ver1.0/lib/my_dll.pyd
      • /ver1.1/lib/my_dll.pyd
    • my_dll.pyd 从辅助文件导入为 from my_dll import * ,我无法控制该方法,也无权更改

    我可以成功地在两种模式之间切换 pyd先生 版本如下

    import sys
    sys.path.append(r'\path\to\ver_1.0')
    import my_dll                    # imports ver 1.0
    print my_dll.release_version()
    sys.path.pop(-1)
    del sys.modules['my_dll']        # remove module dict ref
    from IPython import get_ipython  # just in case you run in iPython
    get_ipython().magic('reset -sf') # need to remove history!
    del my_dll                       # delete the import object
    
    import _ctypes                   # release the library handle
    import ctypes
    dll = ctypes.CDLL('my_dll.pyd')
    _ctypes.FreeLibrary(dll._handle) # for some reason it needs to be
    _ctypes.FreeLibrary(dll._handle) # done twice.
    
    sys.path.append(r'\path\to\ver_1.1')
    import my_dll
    print my_dll.release_version()
    

    失败 reload 通过重新加载C++来克服 pyd先生 根据以下输出

    Version 1.0
    Version 1.1
    

    我的问题是,这样一来,我就剩下一个极其脆弱的python生态系统,几乎在任何新函数调用时都会崩溃。

    是否有更新的方法来实现这一点?

    工具书类

    0 回复  |  直到 8 年前