-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathpythonworker.cpp
More file actions
48 lines (41 loc) · 1.32 KB
/
pythonworker.cpp
File metadata and controls
48 lines (41 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include "PythonAccess/emb.h"
#include "pythonworker.h"
PythonWorker::PythonWorker(QObject *parent) : QObject(parent) {
this->killed.store(-2);
}
void PythonWorker::RunPython(const QString &startme, const QString &code) {
emit StartPythonRun();
PyImport_AppendInittab("emb", emb::PyInitEmbConnect);
PyImport_AppendInittab("express_api", emb::PyInitApiConnection);
Py_Initialize();
PyImport_ImportModule("emb");
emb::StdOutWriteType write = [this](std::string s) {
emit this->WriteOutput(QString::fromStdString(s));
QThread::msleep(10);
};
emb::IsInterruptedType isInterrupted = [this]() {
return this->killed.load();
};
emb::SetStdout(write);
emb::SetIsInterruptedCallback(isInterrupted);
this->killed.store(0);
m_gil = PyGILState_Ensure();
PyRun_SimpleString(startme.toStdString().c_str());
PyGILState_Release(m_gil);
emb::ResetStdOut();
Py_Finalize();
emit EndPythonRun();
}
// https://stackoverflow.com/questions/1420957/stopping-embedded-python
int quit(void *) {
PyErr_SetString(PyExc_KeyboardInterrupt, "...");
return -1;
}
void PythonWorker::StopPython() {
QThread::msleep(2000);
PyGILState_STATE gil;
gil = PyGILState_Ensure();
Py_AddPendingCall(&quit, NULL);
PyGILState_Release(gil);
QThread::msleep(10);
}