本文整理汇总了C++中PyEval_ReleaseLock函数的典型用法代码示例。如果您正苦于以下问题:C++ PyEval_ReleaseLock函数的具体用法?C++ PyEval_ReleaseLock怎么用?C++ PyEval_ReleaseLock使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了PyEval_ReleaseLock函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: PyEval_AcquireLock
bool KviPythonInterpreter::init()
{
// get the global lock
PyEval_AcquireLock();
// get a reference to the PyInterpreterState
PyInterpreterState * mainInterpreterState = mainThreadState->interp;
// create a thread state object for this thread
m_pThreadState = PyThreadState_New(mainInterpreterState);
// swap in the current thread state
PyThreadState_Swap(m_pThreadState);
// and hook in the kvirc error handling routines
QString szPreCode = QString( \
"import kvirc\n" \
"import sys\n" \
"class kvirc_stderr_grabber:\n" \
"\tdef write(self,s):\n" \
"\t\tkvirc.error(s)\n" \
"sys.stderr=kvirc_stderr_grabber()\n"
);
// evaluate that
PyRun_SimpleString(szPreCode.toUtf8().data());
// swap out our thread state for now
PyThreadState_Swap(NULL);
// free the lock
PyEval_ReleaseLock();
return true;
}
示例2: python_init
static int python_init(void)
{
int i;
static char name[] = "radiusd";
if (radiusd_module) return 0;
Py_SetProgramName(name);
Py_Initialize();
PyEval_InitThreads(); /* This also grabs a lock */
if ((radiusd_module = Py_InitModule3("radiusd", radiusd_methods,
"FreeRADIUS Module.")) == NULL)
goto failed;
for (i = 0; radiusd_constants[i].name; i++)
if ((PyModule_AddIntConstant(radiusd_module,
radiusd_constants[i].name,
radiusd_constants[i].value)) < 0)
goto failed;
PyEval_ReleaseLock(); /* Drop lock grabbed by InitThreads */
radlog(L_DBG, "python_init done");
return 0;
failed:
python_error();
Py_XDECREF(radiusd_module);
radiusd_module = NULL;
Py_Finalize();
return -1;
}
示例3: PyEval_AcquireLock
void XBPython::Finalize()
{
if (m_bInitialized)
{
CLog::Log(LOGINFO, "Python, unloading python shared library because no scripts are running anymore");
PyEval_AcquireLock();
PyThreadState_Swap((PyThreadState*)m_mainThreadState);
Py_Finalize();
PyEval_ReleaseLock();
#if !(defined(__APPLE__) || defined(_WIN32))
UnloadExtensionLibs();
#endif
// first free all dlls loaded by python, after that python24.dll (this is done by UnloadPythonDlls
#if !(defined(__APPLE__) || defined(_WIN32))
DllLoaderContainer::UnloadPythonDlls();
#endif
#if defined(_LINUX) && !defined(__APPLE__)
// we can't release it on windows, as this is done in UnloadPythonDlls() for win32 (see above).
// The implementation for linux needs looking at - UnloadPythonDlls() currently only searches for "python24.dll"
// The implementation for osx can never unload the python dylib.
DllLoaderContainer::ReleaseModule(m_pDll);
#endif
m_hModule = NULL;
m_mainThreadState = NULL;
m_bInitialized = false;
}
}
示例4: __tr2qs_ctx
bool KviPythonInterpreter::execute(
const QString &szCode,
QStringList &lArgs, //args
QString &szRetVal,
QString &szError,
QStringList &) //lWarnings
{
if(!m_pThreadState)
{
szError = __tr2qs_ctx("Internal error: python interpreter not initialized","python");
return false;
}
int retVal;
g_lError.clear();
// grab the global interpreter lock
PyEval_AcquireLock();
// swap in my thread state
PyThreadState_Swap(m_pThreadState);
QString szVarCode = "aArgs = [";
bool bFirst = true;
foreach(QString szArg,lArgs)
{
if(!bFirst)
szVarCode += ",";
else
bFirst = false;
szVarCode += QString::fromLatin1("\"%1\"").arg(szArg);
}
szVarCode += "]";
PyRun_SimpleString(szVarCode.toUtf8().data());
//clean "cr" from the python code (ticket #1028)
QString szCleanCode = szCode;
szCleanCode.replace(QRegExp("\r\n?"), "\n");
// execute some python code
retVal = PyRun_SimpleString(szCleanCode.toUtf8().data());
szRetVal.setNum(retVal);
if (PyErr_Occurred() || retVal)
{
szError = g_lError;
}
// clear the thread state
PyThreadState_Swap(NULL);
// release our hold on the global interpreter
PyEval_ReleaseLock();
if(retVal)
return false;
return true;
}
示例5: PyEval_ReleaseLock
void XBPyThread::OnException()
{
done = true;
m_threadState = NULL;
CLog::Log(LOGERROR,"%s, abnormally terminating python thread", __FUNCTION__);
PyEval_ReleaseLock();
m_pExecuter->setDone(m_id);
}
示例6: motor_env_init_v_multi_thread
void
motor_env_init_v_multi_thread()
{
motor_env_init();
if(!PyEval_ThreadsInitialized())
PyEval_InitThreads();
mainThreadState = PyThreadState_Get();
PyEval_ReleaseLock();
}
示例7: Py_InitializeEx
void ModuleLoader::init()
{
Py_InitializeEx(0);
PyEval_InitThreads(); //note, this implicitly acquires the lock!
g_pymaintstate = PyThreadState_Get();
PyEval_ReleaseLock();
}
示例8: assert
/**
* Execute the current script
* We are now in the thread.
*/
void PyApi::ExecuteInThread() const
{
assert(Py_IsInitialized() );
// get the lock so we can change things.
PyEval_AcquireLock();
// make sure that the main thread is the active one.
const auto mainInterpreterState = _mainThreadState->interp;
PyThreadState_Swap(_mainThreadState);
// create a new thread.
const auto myThreadState = PyThreadState_New(mainInterpreterState);
// make sure that the new thread has control
// https://docs.python.org/3/c-api/init.html
PyThreadState_Swap(myThreadState);
// execute it...
{
const auto main_module = PyImport_AddModule("__main__");
const auto main_dict = PyModule_GetDict(main_module);
Py_XINCREF(main_module);
const auto local_dic = PyDict_New();
Py_XINCREF(local_dic);
// we can now run our script
const auto s = _script.c_str();
const auto pyRes = PyRun_String(s, Py_file_input, main_dict, local_dic);
CheckForPythonErrors();
PyDict_Clear(local_dic);
Py_XDECREF(local_dic);
// pending calls must be cleared out
Py_XDECREF(main_module);
}
// swap back to this thread.
PyThreadState_Swap(myThreadState);
// clear anything left behind.
PyThreadState_Clear(myThreadState);
PyThreadState_Swap(nullptr);
// delete my thread.
PyThreadState_Delete(myThreadState);
// give control back to main thread
PyThreadState_Swap(_mainThreadState);
// release the lock one last time.
PyEval_ReleaseLock();
}
示例9: PyThreadState_Swap
void XBPyThread::OnException()
{
PyThreadState_Swap(NULL);
PyEval_ReleaseLock();
CSingleLock lock(m_pExecuter->m_critSection);
m_threadState = NULL;
CLog::Log(LOGERROR,"%s, abnormally terminating python thread", __FUNCTION__);
m_pExecuter->setDone(m_id);
}
示例10: rw_seek_th
static int rw_seek_th(SDL_RWops* context, int offset, int whence)
{
RWHelper* helper = (RWHelper*)context->hidden.unknown.data1;
PyObject* result;
int retval;
PyThreadState* oldstate;
if(!helper->seek || !helper->tell)
return -1;
PyEval_AcquireLock();
oldstate = PyThreadState_Swap(helper->thread);
if(!(offset == 0 && whence == SEEK_CUR)) /*being called only for 'tell'*/
{
result = PyObject_CallFunction(helper->seek, "ii", offset, whence);
if(!result) {
PyErr_Clear();
PyThreadState_Swap(oldstate);
PyEval_ReleaseLock();
return -1;
}
Py_DECREF(result);
}
result = PyObject_CallFunction(helper->tell, NULL);
if(!result) {
PyThreadState_Swap(oldstate);
PyEval_ReleaseLock();
return -1;
}
retval = PyInt_AsLong(result);
Py_DECREF(result);
PyThreadState_Swap(oldstate);
PyEval_ReleaseLock();
return retval;
}
示例11: PyThreadState_Swap
PythonThreadState::~PythonThreadState()
{
if(m_thisThreadState)
{
PyThreadState_Swap(m_mainThreadState);
PyThreadState_Clear(m_thisThreadState);
PyThreadState_Delete(m_thisThreadState);
PyEval_ReleaseLock();
}
}
示例12: gil_real_release
void gil_real_release() {
//uwsgi_log("UNLOCK %d\n", uwsgi.mywid);
#if !defined(PYTHREE) && !defined(UWSGI_PYPY)
pthread_setspecific(up.upt_gil_key, (void *) PyThreadState_Swap(NULL));
PyEval_ReleaseLock();
#else
pthread_setspecific(up.upt_gil_key, (void *) PyThreadState_Get());
PyEval_SaveThread();
#endif
}
示例13: do_python_cleanup
/** Cleanup any thread local storage on pthread_exit()
*/
static void do_python_cleanup(void *arg)
{
PyThreadState *my_thread_state = arg;
PyEval_AcquireLock();
PyThreadState_Swap(NULL); /* Not entirely sure this is needed */
PyThreadState_Clear(my_thread_state);
PyThreadState_Delete(my_thread_state);
PyEval_ReleaseLock();
}
示例14: PythonEnv_Init
PythonEnv* PythonEnv_Init()
{
PythonEnv* Self = (PythonEnv*) malloc( sizeof( PythonEnv ) );
Py_Initialize();
PyEval_InitThreads();
Self->MainThreadState = PyThreadState_Get();
PyEval_ReleaseLock();
return Self;
}
示例15: BPY_thread_save
/* analogue of PyEval_SaveThread() */
BPy_ThreadStatePtr BPY_thread_save(void)
{
PyThreadState *tstate = PyThreadState_Swap(NULL);
/* note: tstate can be NULL when quitting Blender */
if (tstate && PyEval_ThreadsInitialized()) {
PyEval_ReleaseLock();
}
return (BPy_ThreadStatePtr)tstate;
}