add timeout for flushing output buffer when running problem cases

This commit is contained in:
royqh1979@gmail.com 2022-01-28 08:21:56 +08:00
parent c45e0db7c7
commit a9f7cdd0e6
5 changed files with 80 additions and 9 deletions

View File

@ -38,6 +38,7 @@ ExecutableRunner::ExecutableRunner(const QString &filename, const QString &argum
mStartConsole(false),
mQuitSemaphore(0)
{
setWaitForFinishTime(1000);
}
bool ExecutableRunner::startConsole() const
@ -170,7 +171,7 @@ void ExecutableRunner::run()
}
while (true) {
mProcess->waitForFinished(1000);
mProcess->waitForFinished(mWaitForFinishTime);
if (mProcess->state()!=QProcess::Running) {
break;
}

View File

@ -27,6 +27,9 @@ OJProblemCasesRunner::OJProblemCasesRunner(const QString& filename, const QStrin
Runner(filename,arguments,workDir,parent)
{
mProblemCases = problemCases;
mBufferSize = 8192;
mOutputRefreshTime = 1000;
setWaitForFinishTime(100);
}
OJProblemCasesRunner::OJProblemCasesRunner(const QString& filename, const QString& arguments, const QString& workDir,
@ -34,6 +37,9 @@ OJProblemCasesRunner::OJProblemCasesRunner(const QString& filename, const QStrin
Runner(filename,arguments,workDir,parent)
{
mProblemCases.append(problemCase);
mBufferSize = 8192;
mOutputRefreshTime = 1000;
setWaitForFinishTime(100);
}
void OJProblemCasesRunner::runCase(int index,POJProblemCase problemCase)
@ -80,9 +86,10 @@ void OJProblemCasesRunner::runCase(int index,POJProblemCase problemCase)
QByteArray readed;
QByteArray buffer;
QByteArray output;
int noOutputTime = 0;
while (true) {
process.waitForFinished(100);
readed = process.read(5001);
process.waitForFinished(mWaitForFinishTime);
readed = process.read(mBufferSize);
buffer += readed;
if (process.state()!=QProcess::Running) {
break;
@ -97,11 +104,16 @@ void OJProblemCasesRunner::runCase(int index,POJProblemCase problemCase)
}
if (errorOccurred)
break;
if (buffer.length()>5000) {
if (buffer.length()>=mBufferSize || noOutputTime > mOutputRefreshTime) {
if (!buffer.isEmpty()) {
emit newOutputGetted(problemCase->getId(),QString::fromLocal8Bit(buffer));
output.append(buffer);
buffer.clear();
}
noOutputTime = 0;
} else {
noOutputTime += mWaitForFinishTime;
}
}
if (process.state() == QProcess::ProcessState::NotRunning)
buffer += process.readAll();
@ -146,4 +158,34 @@ void OJProblemCasesRunner::run()
}
}
int OJProblemCasesRunner::waitForFinishTime() const
{
return mWaitForFinishTime;
}
void OJProblemCasesRunner::setWaitForFinishTime(int newWaitForFinishTime)
{
mWaitForFinishTime = newWaitForFinishTime;
}
int OJProblemCasesRunner::outputRefreshTime() const
{
return mOutputRefreshTime;
}
void OJProblemCasesRunner::setOutputRefreshTime(int newOutputRefreshTime)
{
mOutputRefreshTime = newOutputRefreshTime;
}
int OJProblemCasesRunner::bufferSize() const
{
return mBufferSize;
}
void OJProblemCasesRunner::setBufferSize(int newBufferSize)
{
mBufferSize = newBufferSize;
}

View File

@ -29,6 +29,17 @@ public:
const QVector<POJProblemCase>& problemCases, QObject *parent = nullptr);
explicit OJProblemCasesRunner(const QString& filename, const QString& arguments, const QString& workDir,
POJProblemCase problemCase, QObject *parent = nullptr);
//max size of output buffer
int bufferSize() const;
void setBufferSize(int newBufferSize);
//max time (in milliseconds) waiting to flush output buffer
int outputRefreshTime() const;
void setOutputRefreshTime(int newOutputRefreshTime);
int waitForFinishTime() const;
void setWaitForFinishTime(int newWaitForFinishTime);
signals:
void caseStarted(const QString& id, int current, int total);
void caseFinished(const QString& id, int current, int total);
@ -41,7 +52,9 @@ private:
// QThread interface
protected:
void run() override;
private:
int mBufferSize;
int mOutputRefreshTime;
};
#endif // OJPROBLEMCASESRUNNER_H

View File

@ -23,7 +23,8 @@ Runner::Runner(const QString &filename, const QString &arguments, const QString
mStop(false),
mFilename(filename),
mArguments(arguments),
mWorkDir(workDir)
mWorkDir(workDir),
mWaitForFinishTime(1000)
{
}
@ -49,3 +50,13 @@ void Runner::setPausing(bool newCanFinish)
mPausing = newCanFinish;
}
int Runner::waitForFinishTime() const
{
return mWaitForFinishTime;
}
void Runner::setWaitForFinishTime(int newWaitForFinishTime)
{
mWaitForFinishTime = newWaitForFinishTime;
}

View File

@ -27,6 +27,9 @@ public:
bool pausing() const;
// time (in milliseconds) waiting for process finished in each processing loop
int waitForFinishTime() const;
void setWaitForFinishTime(int newWaitForFinishTime);
signals:
void started();
@ -45,6 +48,7 @@ protected:
QString mFilename;
QString mArguments;
QString mWorkDir;
int mWaitForFinishTime;
};
#endif // RUNNER_H