Skip to content

Commit fe93c25

Browse files
committed
Proper kill for python code
Signed-off-by: Bhathiya Perera <[email protected]>
1 parent 886baf8 commit fe93c25

File tree

3 files changed

+18
-6
lines changed

3 files changed

+18
-6
lines changed

PythonAccess/pythonworker.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,16 @@ void PythonWorker::RunPython(const QString &startme, const QString &code) {
2222
Py_Finalize();
2323
emit EndPythonRun();
2424
}
25+
26+
// https://stackoverflow.com/questions/1420957/stopping-embedded-python
27+
int quit(void *) {
28+
PyErr_SetInterrupt();
29+
return -1;
30+
}
31+
32+
void PythonWorker::StopPython()
33+
{
34+
QThread::msleep(10);
35+
Py_AddPendingCall(&quit, NULL);
36+
QThread::msleep(10);
37+
}

PythonAccess/pythonworker.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ class PythonWorker : public QObject {
2121

2222
public slots:
2323
void RunPython(const QString &startme, const QString &code);
24+
public:
25+
void StopPython();
2426
};
2527

2628
#endif // PYTHONWORKER_H

UI/mainview.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ MainView::MainView(QWidget *parent)
2121
*/
2222
void MainView::SetupPython() {
2323
emb::setMainView(this);
24-
PythonWorker *worker = new PythonWorker();
24+
PythonWorker* worker = new PythonWorker();
2525
emb::setWorker(worker);
2626
m_workerThread = new QThread();
2727
worker->moveToThread(m_workerThread);
@@ -549,9 +549,6 @@ void MainView::on_btnTuteMark_clicked()
549549

550550
void MainView::on_btnStopPython_clicked()
551551
{
552-
m_workerThread->terminate();
553-
this->EndPythonRun();
554-
m_workerThread->wait();
555-
delete m_workerThread;
556-
SetupPython();
552+
PythonWorker p;
553+
p.StopPython();
557554
}

0 commit comments

Comments
 (0)