Skip to content

Commit

Permalink
Multiprocessing support
Browse files Browse the repository at this point in the history
However several drawbacks:
- No text output from inner process. (If using threading class instead; there is an output)
- Terminate() crashes the whole program. (Using threading, one cannot stop the thread easily)
  • Loading branch information
LaTruelle committed Jun 22, 2015
1 parent 84f7bd7 commit 73e8f1e
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 15 deletions.
3 changes: 1 addition & 2 deletions src/main-window.cc
Original file line number Diff line number Diff line change
Expand Up @@ -733,8 +733,7 @@ void CMainWindow::cancelProcess()
{
if (future.isRunning())
{
qDebug() << "Stopping process";
// TODO: Implement cancelation through python
m_builder->stopBuilding();
}
}

Expand Down
20 changes: 10 additions & 10 deletions src/make-songbook-process.cc
Original file line number Diff line number Diff line change
Expand Up @@ -57,34 +57,30 @@ void CMakeSongbookProcess::stdErr(QString string)
{
// Hack to simplify output
if (!string.simplified().isEmpty()) {
qWarning() << string.simplified().toUtf8().constData();
emit message("PY: " + string.simplified(),0);
// qWarning() << string.simplified().toUtf8().constData();
}
}

void CMakeSongbookProcess::execute()
{
/*
// TESTING TESTING TESTING
qDebug() << "++++++";
QString script = QString::fromLatin1("print('é')");
pythonModule.evalScript(script);
*/

emit(aboutToStart());
if (!m_songbook->filename().isEmpty()) {
// pythonModule.evalScript(QString("setupSongbook('%1','%2')").arg(m_songbook,m_datadirs.first()));
// pythonModule.evalScript("build(['tex', 'pdf', 'sbx', 'pdf', 'clean'])");
// Expose Songbook to python
pythonModule.addObject("songbook", m_songbook);
pythonModule.addObject("CPPprocess", this);
pythonModule.evalScript("print(songbook.filename)");
pythonModule.evalScript("setupSongbook(songbook.filename,'" + m_datadirs.first() + "')");
pythonModule.evalScript("build(['tex', 'pdf', 'sbx', 'pdf', 'clean'])");
pythonModule.removeVariable("songbook");
// pythonModule.removeVariable("songbook");
emit(message("Finished Execution",0));
emit(finished());
}
else{
emit(message("Error: no songbook loaded",0));
emit(finished());
}
}

Expand Down Expand Up @@ -127,7 +123,11 @@ void CMakeSongbookProcess::setWorkingDirectory(const QString &dir)
{
// TODO Do Something ?
pythonModule.evalScript("os.chdir('" + dir + "')");
pythonModule.evalScript("print('CWD: ' + os.getcwd())");
}

void CMakeSongbookProcess::stopBuilding()
{
pythonModule.evalScript("stopBuild()");
}

const CSongbook* CMakeSongbookProcess::songbook() const
Expand Down
5 changes: 5 additions & 0 deletions src/make-songbook-process.hh
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ public slots:
*/
void setUrlToOpen(const QUrl &url);

/*!
Stop the build
*/
void stopBuilding();

public:
/// Constructor.
CMakeSongbookProcess(QObject *parent = 0);
Expand Down
31 changes: 28 additions & 3 deletions src/songbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import os.path
import textwrap
import sys
import logging
import multiprocessing

# Import patacrep modules
from patacrep.build import SongbookBuilder, DEFAULT_STEPS
Expand All @@ -19,6 +21,7 @@

# Define global variables
sb_builder = None
process = None

# Define locale according to user's parameters
def setLocale():
Expand Down Expand Up @@ -76,10 +79,32 @@ def setupSongbook(songbook_path,datadir):
# Deal with error

def build(steps):
global process
process = multiprocessing.Process(target=buildSongbook, args=(steps,))
try:
#logger.info('Starting process')
process.start()
except multiprocessing.ProcessError as error:
#logger.warning('process Error occured')
message(error)
process.join()

def message(text):
CPPprocess.message(text,0)

def buildSongbook(steps):
message("Inner Function Reached")
sys.stdout.flush()
global sb_builder
try:
message("Building songbook")
sb_builder.build_steps(steps)
except errors.SongbookError as error:
print("Building error")
print(error)
# Deal with error
message("Building error")
# Call proper function in CPPprocess
message(error)

def stopBuild():
message("Terminating process")
global process
process.terminate()

0 comments on commit 73e8f1e

Please sign in to comment.