2017-03-21 1 views
0

cythonized python 함수가 포함 된 DLL을 호출하는 C++ 코드가 있습니다. 파이썬 함수는 DataFrame과 함께 작동합니다 (만들고 그 다음에 조회/처리). DataFrame 생성을 한 번만하고 싶습니다. 즉, 파이썬 함수가 종료 된 후에도 상태를 유지해야합니다.cython 호출 사이에 DataFrame 상태 유지

DataFrame 포인터를 cython 팩토리에서 C++로 반환하고 나중에 C++에서 다른 cython 함수로 보내는 방법을 찾지 못했습니다. cython에서 싱글 톤 같은 솔루션을 피하고 싶습니다. 제발 조언.

EDIT1 : foo.pyx :

cdef public string Foo(const string& csvpath) except *: 
    cdef string c_csvpath = csvpath 
    #... 

foo.h : 나는 당신이 다음하지 않으면 당신이 할 수있는합니다 (string 반환 유형을 유지하려는 가정거야

__PYX_EXTERN_C DL_IMPORT(std::string) Foo(std::string const &); 
+0

dll을 어떻게 호출하는지 (단순화 된 버전) 표시 할 수 있습니까? is [public] 또는 [api] 중 하나를 사용하고 있습니까 (https://cython.readthedocs.io/en/latest/src/userguide/external_C_code.html#using-cython-declarations-from-c)? 'DataFrame'을 저장하는 것은 꽤 쉬운 일이라고 생각하지만 지금 당장 무엇을했는지 짐작하기는 어렵습니다. – DavidW

+0

"공개"통화를하고 있습니다. –

답변

0

그냥 쉽게 찾을 수있는 Python 객체를 반환합니다). 이 경우 함수 인수 중 하나를 사용하여 데이터를 저장해야합니다. 모든 변경 가능한 파이썬 객체는 원칙적으로 사용할 수 있지만, 나는 그것이 가장 의미있게 생각하기 때문에 사전에 입증하는거야 :

cdef public string Foo(const string& csvpath, dict saved_data) except *: 
    cdef string c_csvpath = csvpath 

    # get the DataFrame if possible, otherwise generate it 
    try: 
     df = saved_data['dataframe'] 
    except KeyError: 
     df = 3.3 # somehow generate your dataframe 


    # at the end make sure everything's updated 
    saved_data['dataframe'] = df 
    return csvpath 

는 C 서명이된다 :

당신의 C에서
__PYX_EXTERN_C DL_IMPORT(std::string) Foo(std::string const &, PyObject *); 

++ 코드를 사전을 만들고 저장해야합니다.

PyObject* data = PyDict_New(); 
// good code would check for null here 

string out = Foo(string("Hi"),data); 

// your data dictionary should now have "dataframe" in it 

// a second call, reusing the data frame 
string out2 = Foo(string("Hi"),data); 

// once you're sure you've done with the data frame 
Py_DECREF(data); // frees it, unless Python also has a copy 
+0

많은 감사하겠습니다. –