Как открыть ('file-path', 'rb') в файле Python, который встроен в C ++? - PullRequest
1 голос
/ 23 мая 2019

У меня есть некоторый код C ++, который вызывает функцию внутри файла Python.Если я пытаюсь прочитать маринованный файл в коде Python, я получаю следующую ошибку:

Exception ignored in: <module 'threading' from 'C:\\Anaconda3\\envs\\Deep_Learning\\lib\\threading.py'>
Traceback (most recent call last):
  File "C:\Anaconda3\envs\Deep_Learning\lib\threading.py", line 1289, in _shutdown
    assert tlock.locked()
SystemError: <built-in method locked of _thread.lock object at 0x000002328D1EAAA8> returned a result with an error set

Файл Python:

def test():
    print("In function test of pyemb.py file \n")    
    import pickle
    with open('filepath', 'rb') as f_in:
        C = pickle.load(f_in)

C ++(Этот файл вызывает файл python)

#include <stdio.h>
#include <conio.h>
#include <pyhelper.hpp>

#include <iostream>

int main()
{
    CPyInstance hInstance;


    CPyObject pName = PyUnicode_FromString("pyemb");
    CPyObject pModule = PyImport_Import(pName);       //importing the file

    if (pModule) 
    {
        std::cout << "Module load successful" << std::endl;

        CPyObject pFunc1 = PyObject_GetAttrString(pModule, "test"); //passing name of function to be called
        if (pFunc1 && PyCallable_Check(pFunc1)) // checking if not null and callable
        {
            PyObject_CallObject(pFunc1, NULL); 
        }
        else
        {
            printf("ERROR: function test didn't run as intended\n");
        }
    }
    else
    {
        printf_s("ERROR: Module not imported\n");
    }
    return 0;
}

C ++ (pyhelper.hpp для обработки Py_Initialize, Py_Finalize и т. Д.)

#ifndef PYHELPER_HPP
#define PYHELPER_HPP
#pragma once

#include <Python.h>

// This will take care of closing the session when CPyInstance scope ends,
// as destructor will be implicitly called to close the Py instance
class CPyInstance
{
public:
    CPyInstance()
    {
        // Setting Python home path for reading lib and bin
        Py_SetPythonHome(const_cast<wchar_t*>(L"C:\\Anaconda3\\envs\\Deep_Learning"));

        // Initializing Python interpreter
        Py_Initialize(); 

        // Setting path for python to find our python model files 
        PyRun_SimpleString("import sys");
        PyRun_SimpleString("sys.path.append(\"C:\\python_files\")");
    }

    ~CPyInstance()
    {
        Py_Finalize(); // closes the python interpreter instance
    }
};

...More code for other stuff...

Как можноЯ это исправлю?

Пожалуйста, помогите.

...