DLL插件正在冻结.exe程序

时间:2014-03-13 01:20:14

标签: python c++ multithreading dll exe

我正在为.exe程序制作一个C ++ .dll插件。在某些时候,它必须达到python算法(我选择嵌入python)并在插件界面中提供它的解决方案。

由于我还没有集成算法,我制作了一个简单的python脚本来测试嵌入。到目前为止一切正常。

但是当我在这个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_python_embed”)且信息正确

0 个答案:

没有答案