我使用 ctypes
为 C 库编写了一个 Python 包装器。一切正常,直到我尝试使用 threading
模块。
这是一个重现它的小例子:
foo.c
#include <stdlib.h>
#include <assert.h>
char *foo_init() {
char *result = (char*)malloc(sizeof(char));
assert(result);
return result;
}
char foo_get(const char* p) {
return *p;
}
void foo_set(char *p, char value) {
*p = value;
}
void foo_del(char *p) {
free(p);
}
foo.py
import threading
import ctypes
libfoo=ctypes.CDLL('foo.so')
libfoo.foo_init.restype = ctypes.c_void_p
libfoo.foo_get.restype = ctypes.c_char
class Foo:
def __init__(self):
self.__obj__ = libfoo.foo_init()
def get(self):
return libfoo.foo_get(self.__obj__)
def set(self, value):
libfoo.foo_set(self.__obj__, ctypes.c_char(value))
def __del__(self):
libfoo.foo_del(self.__obj__)
x = Foo()
x.set(b'x')
print(x.get())
def compute():
y = Foo()
y.set(b'y')
print(y.get())
t = threading.Thread(target=compute)
t.start()
t.join()
编译:
gcc -Wall -shared -o foo.so -fPIC foo.c
执行:
export LD_LIBRARY_PATH=.
python3 foo.py
结果:
b'x'
[1] 8627 segmentation fault python3 foo.py
我们在这里看到 x.get()
被正确打印,而 y.get()
出现了段错误。因此,似乎存在与 ctypes
和 threading
相关的问题。
请注意,如果我将 y
的初始化移到 compute
函数之外,那么程序会正常退出。
此外,每个操作指针的函数都会产生段错误(例如,如果我只保留 __init__
和 __del__
函数,仍然会发生崩溃)。
知道如何解决这个问题吗?
最佳答案
@user2357112 在评论中给了我答案:添加(好的)返回类型和参数类型很重要。
将 foo.py
的开头替换为以下内容可解决问题:
libfoo.foo_init.restype = ctypes.POINTER(ctypes.c_char)
libfoo.foo_init.argtypes = []
libfoo.foo_get.restype = ctypes.c_char
libfoo.foo_get.argtypes = [ctypes.POINTER(ctypes.c_char)]
libfoo.foo_set.argtypes = [ctypes.POINTER(ctypes.c_char), ctypes.c_char]
libfoo.foo_del.argtypes = [ctypes.POINTER(ctypes.c_char)]
关于python - 使用 ctypes 和线程时出现段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37953055/