Made the interval that the Python profiler runs at configurable
Added a "User parameter:BaseApp/Preferences/PythonConsole/ProfilerInterval" int property which sets how often (in milliseconds) the Python profiler runs while Python code is running. Setting this value to zero will totally disable it. Also added a preference in the Python console screen which allows the user to set the value of this property to between 0 (disabled) and 5000 (once every 5 seconds). (+1 squashed commits) Squashed commits: [cca88ac633] Made the Python profiler only run when the console is running code This has two purposes. First, it prevents a performance impact from running the profiler whenever Python code is running. Second, it prevents crashes caused by Qt's process events function being called too frequently. When the Python code is running in the console, it monopolizes the main thread and prevents events from being processed. Therefore, causing events to be processed in the callback should not force events to be processed too frequently, because the normal loop is being prevented by the Python code. (+1 squashed commits) Squashed commits: [45f86917e6] Made long-running Python code not freeze the GUI without multithreading Removed the background thread running Python code and replaced it with a custom profiler which the Python interpreter runs frequently (at every Python opcode I believe) on the main thread whenever Python code is running. The profiler will make Qt process any new events every 200 ms, preventing "App not responding" messages and making sure any Ctrl+C keypresses will be processed. This prevents the previous issue where running anything GUI-related from Python would crash the program (because Qt isn't thread-safe). (+1 squashed commits) Squashed commits: [0ef7d810b3] Made the process of getting thread IDs cross-platform compatible Instead of using <threads.h>, now the standard <thread> header from C++ 11 is used to find the thread ID, since <threads.h> is apparently not available on Windows. (+1 squashed commits) Squashed commits: [74c7b867f2] # This is a combination of 2 commits. Python from the console now runs in the background In a nutshell, all Python code which is input from the interactive console now runs in a seperate QThread which runs in the background, instead of on the UI thread. This means that long operates operations will no longer cause the app to display an "App not responding" message, because the UI thread is now free to keep running unencumbered. However, it is still not possible to run multiple Python statements at the same time. If the user tries to run some Python while a previous statement is still being processed, instead it will display an error message in the console stating that the previous command is still being processed. I also added a seperate QTimer which runs once every 100ms to flush any output from the Python code back to the console. I can't flush the output to the console from the background thread because the relevant Qt5 code is not thread-safe (it causes random segfaults). So I added this timer as a work-around, since it runs in the main UI thread. Implemented Ctrl+C keyboard interrupts in the console This is implemented by detecting a Ctrl+C key event in the Python console in the main Qt UI thread, and sending a keyboard interrupt to the background thread that runs the Python code.
This commit is contained in:
@@ -32,6 +32,7 @@
|
||||
# include <QTextCursor>
|
||||
# include <QTextDocumentFragment>
|
||||
# include <QTextStream>
|
||||
# include <QTime>
|
||||
# include <QUrl>
|
||||
#endif
|
||||
|
||||
@@ -40,6 +41,7 @@
|
||||
|
||||
#include "PythonConsole.h"
|
||||
#include "PythonConsolePy.h"
|
||||
#include "PythonTracing.h"
|
||||
#include "Application.h"
|
||||
#include "CallTips.h"
|
||||
#include "FileDialog.h"
|
||||
@@ -116,12 +118,15 @@ struct PythonConsoleP
|
||||
colormap[QLatin1String("Python error")] = Qt::red;
|
||||
}
|
||||
};
|
||||
|
||||
struct InteractiveInterpreterP
|
||||
{
|
||||
PyObject* interpreter;
|
||||
PyObject* sysmodule;
|
||||
PyObject* interpreter{nullptr};
|
||||
PyObject* sysmodule{nullptr};
|
||||
QStringList buffer;
|
||||
PythonTracing trace;
|
||||
};
|
||||
|
||||
} // namespace Gui
|
||||
|
||||
InteractiveInterpreter::InteractiveInterpreter()
|
||||
@@ -295,6 +300,16 @@ bool InteractiveInterpreter::runSource(const char* source) const
|
||||
return false;
|
||||
}
|
||||
|
||||
bool InteractiveInterpreter::isOccupied() const
|
||||
{
|
||||
return d->trace.isActive();
|
||||
}
|
||||
|
||||
bool InteractiveInterpreter::interrupt() const
|
||||
{
|
||||
return d->trace.interrupt();
|
||||
}
|
||||
|
||||
/* Execute a code object.
|
||||
*
|
||||
* When an exception occurs, a traceback is displayed.
|
||||
@@ -302,6 +317,13 @@ bool InteractiveInterpreter::runSource(const char* source) const
|
||||
*/
|
||||
void InteractiveInterpreter::runCode(PyCodeObject* code) const
|
||||
{
|
||||
if (isOccupied()) {
|
||||
return;
|
||||
}
|
||||
|
||||
d->trace.fetchFromSettings();
|
||||
PythonTracingLocker tracelock(d->trace);
|
||||
|
||||
Base::PyGILStateLocker lock;
|
||||
PyObject *module, *dict, *presult; /* "exec code in d, d" */
|
||||
module = PyImport_AddModule("__main__"); /* get module, init python */
|
||||
@@ -478,6 +500,10 @@ PythonConsole::PythonConsole(QWidget *parent)
|
||||
d->output = d->info;
|
||||
printPrompt(PythonConsole::Complete);
|
||||
loadHistory();
|
||||
|
||||
flusher = new QTimer(this);
|
||||
connect(flusher, &QTimer::timeout, this, &PythonConsole::flushOutput);
|
||||
flusher->start(100);
|
||||
}
|
||||
|
||||
/** Destroys the object and frees any allocated resources */
|
||||
@@ -558,6 +584,12 @@ void PythonConsole::keyPressEvent(QKeyEvent * e)
|
||||
QTextCursor cursor = this->textCursor();
|
||||
QTextCursor inputLineBegin = this->inputBegin();
|
||||
|
||||
if (e->key() == Qt::Key_C && e->modifiers() == Qt::ControlModifier) {
|
||||
if (d->interpreter->interrupt()) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (!cursorBeyond( cursor, inputLineBegin ))
|
||||
{
|
||||
/**
|
||||
@@ -733,6 +765,15 @@ void PythonConsole::onFlush()
|
||||
printPrompt(PythonConsole::Flush);
|
||||
}
|
||||
|
||||
void PythonConsole::flushOutput()
|
||||
{
|
||||
if (d->interpreter->isOccupied()) {
|
||||
if (d->output.length() > 0 || d->error.length() > 0) {
|
||||
printPrompt(PythonConsole::Complete);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Prints the ps1 prompt (>>> ) for complete and ps2 prompt (... ) for
|
||||
* incomplete commands to the console window.
|
||||
*/
|
||||
@@ -823,6 +864,11 @@ void PythonConsole::runSource(const QString& line)
|
||||
return;
|
||||
}
|
||||
|
||||
if (d->interpreter->isOccupied()) {
|
||||
insertPythonError(QString::fromLatin1("Previous command still running!"));
|
||||
return;
|
||||
}
|
||||
|
||||
bool incomplete = false;
|
||||
Base::PyGILStateLocker lock;
|
||||
PyObject* default_stdout = PySys_GetObject("stdout");
|
||||
|
||||
Reference in New Issue
Block a user