DLL插件正在冻结.exe程序

2024-10-02 22:31:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我为.exe程序制作了一个C++ .dll插件。在某种程度上,它必须深入到一个python算法(我选择了嵌入python)中,并在插件接口中提供该算法的解决方案。你知道吗

由于我还没有集成算法,所以我编写了一个简单的python脚本来测试embdeding。到目前为止一切正常。你知道吗

但是当我在这个python脚本中插入15秒的睡眠来模拟算法运行时间时,这个.exe程序会像预期的那样冻结15秒。我认为此时需要引入线程,所以我遵循了this(更明确地说是“多线程Python嵌入”第一种方法部分)。它仍然冻结,现在我被困在这个时代

有什么办法解决这个问题吗?你知道吗

先谢谢你

Python脚本,bridge.py

def test_callsign(b):
fp_txt=open('allomate.txt','w+')
fp_txt.write(b)
time.sleep(15)
fp_txt.write('after sleep')
if b=='MAH543':
    fp_txt.write('\nOne: ')
    fp_txt.close()
    return 1

C++相关代码:

void CPythonPlugIn::printtofile(EuroScopePlugIn::CAircraft Aircraft){

    ofstream textfile;
    textfile.open("FP.txt");


    // Loop over the planes
    for ( Aircraft = AircraftSelectFirst (); Aircraft.IsValid (); Aircraft = AircraftSelectNext (Aircraft))
    {
        // Aircraft position
        EuroScopePlugIn :: CAircraftPositionData    Aircraftpos = Aircraft.GetPosition () ;

        EuroScopePlugIn::CAircraftFlightPlan fp=Aircraft.GetFlightPlan();

        //Callsign
        textfile<<Aircraft.GetCallsign()<<endl;

        if(strncmp(Aircraft.GetCallsign(),"MAH543",6)==0){

            char *argv[4]={"PythonPlugIn2","bridge","test_callsign","MAH543"};
            int i;
            SENDTOPY cmd;
            cmd.argc=4;
            for( i = 0; i < NUM_ARGUMENTS; i++ )
            {
                cmd.argv[i] = argv[i];
            }

            handle=(HANDLE) _beginthread(py_embed,0,&cmd);

        };

    WaitForSingleObject(handle,INFINITE);
    textfile.close();
}

void py_embed (void*data){

SENDTOPY* arg=(SENDTOPY*)data;

ofstream textfile3;
textfile3.open("FP_python_embed.txt");

PyObject *pName, *pModule, *pDict, *pFunc, *pArgs, *pValue;
PyThreadState *mainThreadState,*myThreadState,*tempState;
PyInterpreterState *mainInterpreterState;

if(arg->argc<3){
    printf("Usage: exe_name python_source function_name\n");
    textfile3<<"Usage: exe_name python_source function_name";
}

//To inform the interpreter about paths to Python run-time libraries
Py_SetProgramName(arg->argv[0]);

// Initialize the Python Interpreter
Py_Initialize();
if( !Py_IsInitialized() ){
    cout<<"Can't initialize"<<endl;
    textfile3<<"Can't initialize"<<endl;
}

// Initialize thread support
PyEval_InitThreads();
// Save a pointer to the main PyThreadState object
mainThreadState = PyThreadState_Get();

// Get a reference to the PyInterpreterState
mainInterpreterState = mainThreadState->interp;

// Create a thread state object for this thread
myThreadState = PyThreadState_New(mainInterpreterState);

// Release global lock
PyEval_ReleaseLock();

// Acquire global lock
PyEval_AcquireLock();

// Swap in my thread state
tempState = PyThreadState_Swap(myThreadState);

// Build the name object
pName = PyString_FromString(arg->argv[1]);
if( !pName ){
    cout<<"Can't build the object "<<endl;
    textfile3<<"Can't build the object "<<endl;
}

// Load the module object
pModule = PyImport_Import(pName);
if( !pModule ){
    cout<<"Can't import the module "<<endl;
    textfile3<<"Can't import the module "<<endl;
}

// pDict is a borrowed reference 
pDict = PyModule_GetDict(pModule);
if( !pDict ){
    cout<<"Can't get the dict"<<endl;
    textfile3<<"Can't get the dict"<<endl;
}


// pFunc is also a borrowed reference 
pFunc = PyDict_GetItemString(pDict, arg->argv[2]);
if( !pFunc || !PyCallable_Check(pFunc) ){
    cout<<"can't get the function"<<endl;
    textfile3<<"can't get the function"<<endl;
}



PyObject *pArgs2, *pValue2;
pArgs2=Py_BuildValue("(s)",arg->argv[3]);
pValue2 = PyObject_CallObject(pFunc, pArgs2);
textfile3<<PyInt_AsLong(pValue2)<<endl<<" worked1";
textfile3.close();

// Clean up
Py_DECREF(pModule);
Py_DECREF(pName);

// Swap out the current thread
PyThreadState_Swap(tempState);

// Release global lock
PyEval_ReleaseLock();

// Clean up thread state
PyThreadState_Clear(myThreadState);
PyThreadState_Delete(myThreadState);

// Finish the Python Interpreter
Py_Finalize();

_endthread();
};

编辑:生成三个.txt(“allomate”、“FP”和“FP\u python\u embed”),信息正确


Tags: thepytxtifobjectargthreadcan