Commit 062c0815 authored by NICLAUSSE Nicolas's avatar NICLAUSSE Nicolas

add option to override settings file + add threads related methods in

pythonInterpreter
parent da4175b4
......@@ -48,7 +48,7 @@ public:
public slots:
virtual QString interpret(const QString& command, int *stat) = 0;
virtual void init(bool redirect_io = false) {} ;
virtual void init(bool redirect_io = false, const QString & settings_file = QString("dtk-script")) {} ;
private:
dtkScriptInterpreterPrivate *d;
......
......@@ -55,6 +55,11 @@ public:
QString buffer;
bool redirect_io = false;
public:
PyThreadState *thread_state = nullptr;
long thread_with_gil = -1;
PyGILState_STATE gilState;
public:
PyObject *module = nullptr;
......@@ -78,7 +83,7 @@ dtkScriptInterpreterPython *dtkScriptInterpreterPython::s_instance = nullptr;
dtkScriptInterpreterPython::dtkScriptInterpreterPython(void) : dtkScriptInterpreter(), d(new dtkScriptInterpreterPythonPrivate)
{
Py_Initialize();
}
dtkScriptInterpreterPython::~dtkScriptInterpreterPython(void)
......@@ -87,16 +92,15 @@ dtkScriptInterpreterPython::~dtkScriptInterpreterPython(void)
d = nullptr;
}
void dtkScriptInterpreterPython::init(bool redirect_io)
void dtkScriptInterpreterPython::init(bool redirect_io, const QString& settings_file)
{
Py_Initialize();
d->redirect_io = redirect_io;
if (redirect_io) {
PyRun_SimpleString(dtkScriptInterpreterPythonRedirector_declare.toUtf8().constData());
}
QSettings settings(QSettings::IniFormat, QSettings::UserScope, "inria", "dtk-script");
QSettings settings(QSettings::IniFormat, QSettings::UserScope, "inria", settings_file);
settings.beginGroup("modules");
QString paths = settings.value("path").toString();
settings.endGroup();
......@@ -115,13 +119,45 @@ void dtkScriptInterpreterPython::init(bool redirect_io)
if (!init.isEmpty()) {
PyRun_SimpleString(qPrintable(QString("execfile('%1')").arg(init)));
} else {
dtkWarn() << Q_FUNC_INFO << "No init function";
dtkInfo() << Q_FUNC_INFO << "No init function";
}
if (redirect_io) {
PyRun_SimpleString(dtkScriptInterpreterPythonRedirector_define.toUtf8().constData());
}
}
void dtkScriptInterpreterPython::allowThreads(void)
{
//init python threads
if(!PyEval_ThreadsInitialized()) {
qDebug() << "init threads";
PyEval_InitThreads(); // in 3.7 not needed anymore
}
d->thread_state = PyEval_SaveThread();
}
void dtkScriptInterpreterPython::endAllowThreads(void)
{
if(d->thread_state)
PyEval_RestoreThread(d->thread_state);
d->thread_state = nullptr;
}
void dtkScriptInterpreterPython::childAcquireLock(void)
{
d->thread_with_gil = long(QThread::currentThreadId());
d->gilState = PyGILState_Ensure();
}
void dtkScriptInterpreterPython::childReleaseLock(void)
{
long current_thread = long(QThread::currentThreadId());
if(current_thread == d->thread_with_gil) {
PyGILState_Release(d->gilState);
d->thread_with_gil = -1;
}
}
void dtkScriptInterpreterPython::release(void)
{
Py_Finalize();
......
......@@ -30,7 +30,15 @@ protected:
public slots:
QString interpret(const QString& command, int *stat) override;
void init(bool redirect_io = false) override ;
void init(bool redirect_io = false, const QString& settings_file = QString("dtk-script") ) override ;
public:
void allowThreads(void); // need to be called from main thread
void endAllowThreads(void); // need to be called from main thread
void childAcquireLock(void); // need to be called from child thread
void childReleaseLock(void); // need to be called from child thread
public:
void release(void);
private:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment