Cython 클래스는 기본적으로 pickleable이 아니므로 Pickle interface을 직접 구현해야합니다. 여러 가지 단계를 수행 할 수 있지만 __getstate__
및 __setstate__
은 가장 사용자에게 친숙한 수준이므로 그렇지 않으면 좋은 이유가없는 한 시작하는 것이 좋습니다.
클래스의 내용을 pickleable로 만들려면 __getstate__
에 터플을 반환하고 그 반대로는 __setstate__
으로 반환하는 것만 큼 쉽습니다. Memoryviews 자체는 pickleable이 아니지만 base
속성을 가질 수 있습니다.
cdef class C:
cdef double[:] array
cdef python_obj
cdef int integer
def __init__(self,array,python_obj,integer):
self.array = array
self.python_obj = python_obj
self.integer = integer
def __getstate__(self):
return (self.array.base, # memoryviews aren't pickleable, need to get underlying object
# and hope it's pickleable
self.python_obj, self.integer)
def __setstate__(self,x):
self.array, self.python_obj, self.integer = x
클래스에 C 또는 C++ 객체가 있으면 훨씬 더 복잡합니다. 간단한 유형의 경우 시작하기 좋은 곳은 메모리를 bytearray에 복사하거나 Cython의 기본값 인 struct<->dict
상호 변환을 이용하는 것입니다. 그러나 클래스에 포인터가 포함되어 있으면이 방법이 작동하지 않으므로 C/C++에서 신뢰할 수있는로드/저장 메커니즘을 구현해야합니다.
많은 문서와 이전 질문은 읽지 않은 것처럼 보입니다. 예 : https://docs.python.org/3/library/pickle.html#what-can-be- pickle-and-unpickled ([확장 유형은 기본적으로'__dict__'을 정의하지 않습니다.} (http://cython.readthedocs.io/en/latest/src/reference/extension_types.html#attributes)). 관련성 높은 이전 질문 : http://stackoverflow.com/questions/12646436/pickle-cython-class http://stackoverflow.com/questions/36301322/pickle-cython-class-with-c-pointers – DavidW
또한 제목에는 포인터가 언급되어 있지만 코드에는 아무 것도 표시되지 않습니다. 아마 분명히 해줄까요? – DavidW
@DavidW 고마워. 고마워. –