Skip to main content
 首页 » 编程设计

python之强制 ctypes.cdll.LoadLibrary() 从文件重新加载库

2024年09月07日10lautakyan007

我有以下代码

import ctypes 
lib1 = ctypes.cdll.LoadLibrary("./mylib.so") 
# modify mylib.so (code generation and compilation) or even delete it 
lib2 = ctypes.cdll.LoadLibrary("./mylib.so") 

问题是 lib2 指的是原始共享库,而不是新共享库。如果我在调用之间删除 mylib.so,我不会出错。

使用 ctypes._reset_cache() 没有帮助。

我如何告诉 ctypes 从硬盘实际重新加载库?

请您参考如下方法:

我不知道如何指示 ctypes 如何卸载库(在 [Python.Docs]: ctypes - A foreign function library for Python 上没有找到方法,但这并不意味着没有)。

它可以通过强制加载程序(减少库的引用​​计数并)通过 [Man7]: DLCLOSE(3P) 卸载它来手动完成。 (另请阅读 [Man7]: DLOPEN(3) 以获取有关加载/卸载库的更多信息)。

dll00.c:

#include <stdio.h> 
 
 
int func0(int arg0) 
{ 
    int alter_factor = 2; 
    printf("From C - arg0: %d, alter_factor: %d\n", arg0, alter_factor); 
    return arg0 * alter_factor; 
} 

code00.py:

#!/usr/bin/env python 
 
import ctypes as cts 
import sys 
 
 
DLL_NAME = "./dll00.{:s}".format("dll" if sys.platform[:3].lower() == "win" else "so") 
 
 
def handle_dll(name=DLL_NAME): 
    dll = cts.CDLL(name) 
    func0 = dll.func0 
    func0.argtypes = (cts.c_int,) 
    func0.restype = cts.c_int 
    return dll, func0 
 
 
def main(*argv): 
    dlclose_func = cts.CDLL(None).dlclose 
    dlclose_func.argtypes = (cts.c_void_p,) 
    dlclose_func.restype = cts.c_int 
 
    dll, func0 = handle_dll() 
    res = func0(42) 
    print(res) 
    dlclose_func(dll._handle) 
    input("In another terminal, modify the C code (e.g. change `alter_factor`), recompile (gcc -fPIC -shared -o dll00.so dll00.c), and when done press ENTER here... ") 
    dll, func0 = handle_dll() 
    res = func0(42) 
    print(res) 
 
 
if __name__ == "__main__": 
    print("Python {:s} {:03d}bit on {:s}\n".format(" ".join(elem.strip() for elem in sys.version.split("\n")), 
                                                   64 if sys.maxsize > 0x100000000 else 32, sys.platform)) 
    rc = main(*sys.argv[1:]) 
    print("\nDone.\n") 
    sys.exit(rc) 

输出:

(qaic-env) [cfati@cfati-5510-0:/mnt/e/Work/Dev/StackOverflow/q050964033]> ~/sopr.sh 
### Set shorter prompt to better fit when pasted in StackOverflow (or other) pages ### 
 
[064bit prompt]> ls 
code00.py  dll00.c  dll00.so 
[064bit prompt]> 
[064bit prompt]> python code00.py 
Python 3.8.10 (default, Nov 26 2021, 20:14:08) [GCC 9.3.0] 064bit on linux 
 
From C - arg0: 42, alter_factor: 2 
84 
In another terminal, modify the C code (e.g. change `alter_factor`), recompile (gcc -fPIC -shared -o dll00.so dll00.c), and when done press ENTER here...  
From C - arg0: 42, alter_factor: 3 
126 
 
Done. 

相关(或多或少):