Almost working
This commit is contained in:
@@ -11,6 +11,7 @@ namespace pywrap {
|
||||
PyWrap* PyWrap::wrapper = nullptr;
|
||||
std::mutex PyWrap::mutex;
|
||||
CPyInstance* PyWrap::pyInstance = nullptr;
|
||||
auto moduleClassMap = std::map<std::pair<std::string, std::string>, std::tuple<PyObject*, PyObject*, PyObject*>>();
|
||||
|
||||
PyWrap* PyWrap::GetInstance()
|
||||
{
|
||||
@@ -25,12 +26,21 @@ namespace pywrap {
|
||||
}
|
||||
void PyWrap::RemoveInstance()
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex);
|
||||
if (wrapper != nullptr) {
|
||||
std::cout << "Liberando instancia" << std::endl;
|
||||
delete pyInstance;
|
||||
std::cout << "Liberando instancia Python Stack" << std::endl;
|
||||
if (pyInstance != nullptr) {
|
||||
std::cout << "-Liberando Python => PyHelper" << std::endl;
|
||||
delete pyInstance;
|
||||
} else {
|
||||
std::cout << "*No había instancia de python para liberar. => PyHelper" << std::endl;
|
||||
}
|
||||
pyInstance = nullptr;
|
||||
delete wrapper;
|
||||
if (wrapper != nullptr) {
|
||||
std::cout << "-Liberando PyWrap." << std::endl;
|
||||
delete wrapper;
|
||||
} else {
|
||||
std::cout << "*No había instancia de PyWrap para liberar." << std::endl;
|
||||
}
|
||||
wrapper = nullptr;
|
||||
std::cout << "Instancia liberada" << std::endl;
|
||||
}
|
||||
@@ -55,17 +65,25 @@ namespace pywrap {
|
||||
if (PyErr_Occurred()) {
|
||||
errorAbort("Couldn't create instance of class " + className);
|
||||
}
|
||||
moduleClassMap[{moduleName, className}] = { module, classObject, instance };
|
||||
std::lock_guard<std::mutex> lock(mutex);
|
||||
module.AddRef();
|
||||
classObject.AddRef();
|
||||
instance.AddRef();
|
||||
moduleClassMap.insert({ { moduleName, className }, { module.getObject(), classObject.getObject(), instance.getObject() } });
|
||||
std::cout << "Clase importada" << std::endl;
|
||||
}
|
||||
void PyWrap::clean(const std::string& moduleName, const std::string& className)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex);
|
||||
std::cout << "Limpiando" << std::endl;
|
||||
auto result = moduleClassMap.find({ moduleName, className });
|
||||
if (result == moduleClassMap.end()) {
|
||||
return;
|
||||
}
|
||||
std::cout << "--> Limpiando" << std::endl;
|
||||
Py_DECREF(std::get<0>(result->second));
|
||||
Py_DECREF(std::get<1>(result->second));
|
||||
Py_DECREF(std::get<2>(result->second));
|
||||
moduleClassMap.erase(result);
|
||||
if (PyErr_Occurred()) {
|
||||
PyErr_Print();
|
||||
@@ -122,11 +140,11 @@ namespace pywrap {
|
||||
void PyWrap::fit(const std::string& moduleName, const std::string& className, CPyObject& X, CPyObject& y)
|
||||
{
|
||||
std::cout << "Llamando método fit" << std::endl;
|
||||
CPyObject instance = getClass(moduleName, className);
|
||||
PyObject* instance = getClass(moduleName, className);
|
||||
CPyObject result;
|
||||
std::string method = "fit";
|
||||
try {
|
||||
if (!(result = PyObject_CallMethodObjArgs(instance, PyUnicode_FromString(method.c_str()), X, y, NULL)))
|
||||
if (!(result = PyObject_CallMethodObjArgs(instance, PyUnicode_FromString(method.c_str()), X.getObject(), y.getObject(), NULL)))
|
||||
errorAbort("Couldn't call method fit");
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
@@ -142,7 +160,7 @@ namespace pywrap {
|
||||
CPyObject result;
|
||||
std::string method = "predict";
|
||||
try {
|
||||
if (!(result = PyObject_CallMethodObjArgs(instance, PyUnicode_FromString(method.c_str()), X, NULL)))
|
||||
if (!(result = PyObject_CallMethodObjArgs(instance, PyUnicode_FromString(method.c_str()), X.getObject(), NULL)))
|
||||
errorAbort("Couldn't call method predict");
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
@@ -155,11 +173,11 @@ namespace pywrap {
|
||||
double PyWrap::score(const std::string& moduleName, const std::string& className, CPyObject& X, CPyObject& y)
|
||||
{
|
||||
std::cout << "Llamando método score" << std::endl;
|
||||
CPyObject instance = getClass(moduleName, className);
|
||||
PyObject* instance = getClass(moduleName, className);
|
||||
CPyObject result;
|
||||
std::string method = "score";
|
||||
try {
|
||||
if (!(result = PyObject_CallMethodObjArgs(instance, PyUnicode_FromString(method.c_str()), X, y, NULL)))
|
||||
if (!(result = PyObject_CallMethodObjArgs(instance, PyUnicode_FromString(method.c_str()), X.getObject(), y.getObject(), NULL)))
|
||||
errorAbort("Couldn't call method score");
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
|
Reference in New Issue
Block a user