Skip to content

Commit

Permalink
Switching back to threading module
Browse files Browse the repository at this point in the history
Testing @oliverpool suggestion.
stopBuild() still crashes :-(
  • Loading branch information
LaTruelle committed Nov 2, 2015
1 parent bbe7b81 commit 6a219d3
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 11 deletions.
8 changes: 8 additions & 0 deletions src/patacrep.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "patacrep.hh"

#include <QDebug>

Patacrep::Patacrep(QObject *parent) : QObject(parent)
{
// Setup Python interpreter
Expand All @@ -11,6 +13,7 @@ Patacrep::Patacrep(QObject *parent) : QObject(parent)
SLOT(stdOut(QString)));
connect(PythonQt::self(), SIGNAL(pythonStdErr(QString)),
SLOT(stdErr(QString)));
connect(this, SIGNAL(message(QString, int)), SLOT(debugOutput(QString)));
// Import Python file containing all necessary functions and imports
pythonModule.evalFile(":/python_scripts/songbook.py");
}
Expand Down Expand Up @@ -85,6 +88,11 @@ void Patacrep::buildSongbook()
}
}

void Patacrep::debugOutput(QString string)
{
qDebug() << string;
}

void Patacrep::setDatadirs(const QStringList &datadirs)
{
Patacrep::datadirs.append(datadirs);
Expand Down
2 changes: 2 additions & 0 deletions src/patacrep.hh
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ public slots:

void stdErr(QString string);

void debugOutput(QString string);

private:
PythonQtObjectPtr pythonModule;
Songbook *songbook;
Expand Down
37 changes: 26 additions & 11 deletions src/songbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import textwrap
import sys
import logging
import asyncio
import threading

# Import patacrep modules
from patacrep.build import SongbookBuilder, DEFAULT_STEPS
Expand All @@ -22,8 +22,8 @@
# Define global variables
sb_builder = None
process = None
loop = asyncio.get_event_loop()
# logging.basicConfig(level=logging.DEBUG)
stopProcess = False
logging.basicConfig(level=logging.DEBUG)

# Define locale according to user's parameters
def setLocale():
Expand Down Expand Up @@ -84,15 +84,30 @@ def setupSongbook(songbook_path,datadir):
print("Error in formation of Songbook Builder")
# Deal with error

# Wrapper around buildSongbook that manages the event loop part
# Wrapper around buildSongbook that manages the threading part
def build(steps):
global loop
message("==== Starting Loop call")
loop.run_until_complete(buildSongbook(steps))
message("==== Finished")
global stopProcess
global process
stopProcess = False
process = threading.Thread(target=buildSongbook, args=(steps,))
try:
#logger.info('Starting process')
process.start()
except threading.ThreadError as error:
#logger.warning('process Error occured')
message(error)

period = 2
while process.is_alive():
if stopProcess:
process.terminate()
print("terminated")
# Check in 2 seconds
process.join(period)
message("end build")

# Inner function that actually builds the songbook
async def buildSongbook(steps):
def buildSongbook(steps):
global sb_builder
message("Inner Function Reached")
sys.stdout.flush()
Expand All @@ -109,6 +124,6 @@ async def buildSongbook(steps):
message("Exiting buildSongbook function")

def stopBuild():
global loop
message("Terminating process")
loop.stop()
global stopProcess
stopProcess = True

0 comments on commit 6a219d3

Please sign in to comment.