mirror of
https://github.com/rizinorg/cutter.git
synced 2025-01-19 10:58:51 +00:00
Start Jupyter with single Process
This commit is contained in:
parent
7526965b26
commit
cea55dfdb1
@ -16,17 +16,42 @@ JupyterConnection::~JupyterConnection()
|
|||||||
{
|
{
|
||||||
cmdServer->stop();
|
cmdServer->stop();
|
||||||
process->terminate();
|
process->terminate();
|
||||||
urlProcess->terminate();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const char *urlPy = "from notebook import notebookapp\n"
|
const char *jupyterPyCode = "import sys\n"
|
||||||
"import json\n"
|
"from notebook.notebookapp import *\n"
|
||||||
"import time\n"
|
|
||||||
"\n"
|
"\n"
|
||||||
"time.sleep(3)\n"
|
|
||||||
"\n"
|
"\n"
|
||||||
"servers = [si for si in notebookapp.list_running_servers()]\n"
|
"class CutterNotebookApp(NotebookApp):\n"
|
||||||
"print(json.dumps(servers))";
|
" def start(self):\n"
|
||||||
|
" \"\"\" see NotebookApp.start() \"\"\"\n"
|
||||||
|
"\n"
|
||||||
|
" super(NotebookApp, self).start()\n"
|
||||||
|
"\n"
|
||||||
|
" self.write_server_info_file()\n"
|
||||||
|
"\n"
|
||||||
|
" if self.token and self._token_generated:\n"
|
||||||
|
" url = url_concat(self.connection_url, {'token': self.token})\n"
|
||||||
|
" sys.stdout.write(url + \"\\n\")\n"
|
||||||
|
" sys.stdout.flush()\n"
|
||||||
|
"\n"
|
||||||
|
" self.io_loop = ioloop.IOLoop.current()\n"
|
||||||
|
" if sys.platform.startswith('win'):\n"
|
||||||
|
" # add no-op to wake every 5s\n"
|
||||||
|
" # to handle signals that may be ignored by the inner loop\n"
|
||||||
|
" pc = ioloop.PeriodicCallback(lambda: None, 5000)\n"
|
||||||
|
" pc.start()\n"
|
||||||
|
" try:\n"
|
||||||
|
" self.io_loop.start()\n"
|
||||||
|
" except KeyboardInterrupt:\n"
|
||||||
|
" self.log.info(_(\"Interrupted...\"))\n"
|
||||||
|
" finally:\n"
|
||||||
|
" self.remove_server_info_file()\n"
|
||||||
|
" self.cleanup_kernels()\n"
|
||||||
|
"\n"
|
||||||
|
"\n"
|
||||||
|
"if __name__ == \"__main__\":\n"
|
||||||
|
" CutterNotebookApp.launch_instance()";
|
||||||
|
|
||||||
void JupyterConnection::start()
|
void JupyterConnection::start()
|
||||||
{
|
{
|
||||||
@ -34,11 +59,7 @@ void JupyterConnection::start()
|
|||||||
connect(process, &QProcess::readyReadStandardError, this, &JupyterConnection::readStandardError);
|
connect(process, &QProcess::readyReadStandardError, this, &JupyterConnection::readStandardError);
|
||||||
connect(process, &QProcess::readyReadStandardOutput, this, &JupyterConnection::readStandardOutput);
|
connect(process, &QProcess::readyReadStandardOutput, this, &JupyterConnection::readStandardOutput);
|
||||||
connect(process, &QProcess::errorOccurred, this, [](QProcess::ProcessError error){ qWarning() << "Jupyter error occurred:" << error; });
|
connect(process, &QProcess::errorOccurred, this, [](QProcess::ProcessError error){ qWarning() << "Jupyter error occurred:" << error; });
|
||||||
process->start("jupyter", {"notebook", "--no-browser", "-y"});
|
process->start("python3", {"-c", jupyterPyCode});
|
||||||
|
|
||||||
urlProcess = new QProcess(this);
|
|
||||||
connect(urlProcess, &QProcess::readyReadStandardOutput, this, &JupyterConnection::readUrlStandardOutput);
|
|
||||||
urlProcess->start("python3", {"-c", urlPy});
|
|
||||||
|
|
||||||
QThread *cmdServerThread = new QThread(this);
|
QThread *cmdServerThread = new QThread(this);
|
||||||
cmdServer = new CommandServer();
|
cmdServer = new CommandServer();
|
||||||
@ -54,24 +75,12 @@ void JupyterConnection::start()
|
|||||||
void JupyterConnection::readStandardError()
|
void JupyterConnection::readStandardError()
|
||||||
{
|
{
|
||||||
auto data = process->readAllStandardError();
|
auto data = process->readAllStandardError();
|
||||||
printf("Jupyter stderr: %s", data.constData());
|
printf("Jupyter stderr: %s\n", data.constData());
|
||||||
}
|
}
|
||||||
|
|
||||||
void JupyterConnection::readStandardOutput()
|
void JupyterConnection::readStandardOutput()
|
||||||
{
|
{
|
||||||
auto data = process->readAllStandardOutput();
|
auto data = process->readAllStandardOutput();
|
||||||
printf("Jupyter stdout: %s", data.constData());
|
printf("Jupyter stdout: %s\n", data.constData());
|
||||||
}
|
emit urlReceived(data);
|
||||||
|
|
||||||
void JupyterConnection::readUrlStandardOutput()
|
|
||||||
{
|
|
||||||
QJsonDocument doc = QJsonDocument::fromJson(urlProcess->readAllStandardOutput());
|
|
||||||
|
|
||||||
for(QJsonValue value : doc.array())
|
|
||||||
{
|
|
||||||
QJsonObject serverObject = value.toObject();
|
|
||||||
QString url = serverObject["url"].toString() + "?token=" + serverObject["token"].toString();
|
|
||||||
emit urlReceived(url);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
@ -19,14 +19,11 @@ signals:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
QProcess *process;
|
QProcess *process;
|
||||||
QProcess *urlProcess;
|
|
||||||
CommandServer *cmdServer;
|
CommandServer *cmdServer;
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
void readStandardError();
|
void readStandardError();
|
||||||
void readStandardOutput();
|
void readStandardOutput();
|
||||||
|
|
||||||
void readUrlStandardOutput();
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif //JUPYTERCONNECTION_H
|
#endif //JUPYTERCONNECTION_H
|
||||||
|
Loading…
Reference in New Issue
Block a user