Skip to content

Commit

Permalink
Merge pull request mantidproject#20372 from mantidproject/fix_recursi…
Browse files Browse the repository at this point in the history
…ve_runPythonScript_async

Fix recursive runPythonScript in async mode
  • Loading branch information
peterfpeterson authored Sep 5, 2017
2 parents 0b9be70 + 32ab9c3 commit 60203eb
Show file tree
Hide file tree
Showing 11 changed files with 138 additions and 203 deletions.
1 change: 0 additions & 1 deletion MantidPlot/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -789,7 +789,6 @@ add_executable ( MantidPlot ${WIN_CONSOLE} MACOSX_BUNDLE ${ALL_SRC} src/main.cpp
# Library dependencies
target_link_libraries ( MantidPlot LINK_PRIVATE ${TCMALLOC_LIBRARIES_LINKTIME}
${CORE_MANTIDLIBS}
API
MantidQtWidgetsCommon
MantidQtWidgetsInstrumentView
MantidQtWidgetsSliceViewer
Expand Down
25 changes: 16 additions & 9 deletions MantidPlot/src/ApplicationWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -573,8 +573,8 @@ void ApplicationWindow::init(bool factorySettings, const QStringList &args) {

// Scripting
m_script_envs = QHash<QString, ScriptingEnv *>();
m_iface_script = nullptr;
setScriptingLanguage(defaultScriptingLang);
m_iface_script = NULL;

m_interpreterDock = new QDockWidget(this);
m_interpreterDock->setObjectName(
Expand Down Expand Up @@ -15043,10 +15043,9 @@ bool ApplicationWindow::runPythonScript(const QString &code, bool async,
bool quiet, bool redirect) {
if (code.isEmpty())
return false;

if (m_iface_script == NULL) {
if (!m_iface_script) {
if (setScriptingLanguage("Python")) {
m_iface_script = scriptingEnv()->newScript("<Interface>", NULL,
m_iface_script = scriptingEnv()->newScript("<Interface>", nullptr,
Script::NonInteractive);
} else {
return false;
Expand All @@ -15065,12 +15064,20 @@ bool ApplicationWindow::runPythonScript(const QString &code, bool async,
}
bool success(false);
if (async) {
QFuture<bool> job = m_iface_script->executeAsync(ScriptCode(code));
while (job.isRunning()) {
QCoreApplication::instance()->processEvents();
m_iface_script->recursiveAsyncSetup();
auto job = m_iface_script->executeAsync(ScriptCode(code));
// Start a local event loop to keep processing events
// while we are running the script. Inspired by the IPython
// Qt inputhook in IPython.terminal.pt_inputhooks.qt
QEventLoop eventLoop(QApplication::instance());
QTimer timer;
connect(&timer, SIGNAL(timeout()), &eventLoop, SLOT(quit()));
while (!job.isFinished()) {
timer.start(50);
eventLoop.exec();
timer.stop();
}
// Ensure the remaining events are processed
QCoreApplication::instance()->processEvents();
m_iface_script->recursiveAsyncTeardown();
success = job.result();
} else {
success = m_iface_script->execute(ScriptCode(code));
Expand Down
6 changes: 4 additions & 2 deletions MantidPlot/src/Mantid/MantidUI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,8 @@ void MantidUI::shutdown() {

// If any python objects need to be cleared away then the GIL needs to be
// held.
ScopedPythonGIL gil;
PythonGIL gil;
ScopedPythonGIL lock(gil);
// Relevant notifications are connected to signals that will close all
// dependent windows
Mantid::API::FrameworkManager::Instance().shutdown();
Expand Down Expand Up @@ -2217,7 +2218,8 @@ void MantidUI::clearAllMemory(const bool prompt) {
// If any python objects need to be cleared away then the GIL needs to be
// held. This doesn't feel like
// it is in the right place but it will do no harm
ScopedPythonGIL gil;
PythonGIL gil;
ScopedPythonGIL lock(gil);
// Relevant notifications are connected to signals that will close all
// dependent windows
Mantid::API::FrameworkManager::Instance().clear();
Expand Down
Loading

0 comments on commit 60203eb

Please sign in to comment.