Sequential execution of QProcesses
I have 5 QT processes in a slot, which invokes 5 shell scripts on linux system. I have arranged them in a sequential manner but when I had a look at logs, came to know that the processes are not running in a sequential manner. The order of execution gets change
For eg
Code:
void Window::slotProcess(){
statusWindow->setText("Running process");
/******************* STEP 1: START A NEW PROCESS 1 ***************/
// Connet SIGNAL::STANDARD ERROR with SLOT::UPDATE ERROR, to capture std output
qDebug() << connect(Process_1, SIGNAL(readyReadStandardError()), this, SLOT(UpdateError()));
// Connect SIGNAL::STANDARD OUTPUT with SLOT::UPDATE OUTPUT to capture std error
qDebug() << connect(Process_1, SIGNAL(readyReadStandardOutput()), this, SLOT(UpdateOutput()));
Process_1->start("/home/root/scripts/script_1.sh");
/******************* STEP 2: START A NEW PROCESS 2 ***************/
// Connet SIGNAL::STANDARD ERROR with SLOT::UPDATE ERROR, to capture std output
qDebug() << connect(Process_2, SIGNAL(readyReadStandardError()), this, SLOT(UpdateError()));
// Connect SIGNAL::STANDARD OUTPUT with SLOT::UPDATE OUTPUT to capture std error
qDebug() << connect(Process_2, SIGNAL(readyReadStandardOutput()), this, SLOT(UpdateOutput()));
Process_2->start("/home/root/scripts/script_2.sh");
.....
But when I see debug messages, it shows there is no sequence in execution. How can I control the sequence of execution of my processes?
The wait for finish method will block the code, is there an alternative ?
Re: Sequential execution of QProcesses
Use QProcess::waitForFinished before starting next process.
Oops : You do not want to use a waitForFinished.
Create one master script which will call subscripts.
Re: Sequential execution of QProcesses
Lesiok's option is probably the least effort. Here are some other options:
Create a slot for each step. In the slot for step 1 start the relevant process and connect() its finished() signal to the slot for step 2... and so on. You should check the exit state of step n at the top of step n+1 before starting the new step's process.
Or, between steps:
Code:
connect(process, SIGNAL(finished(...)), &loop, SLOT(quit()));
loop.exec();
Re: Sequential execution of QProcesses
Quote:
Originally Posted by
ChrisW67
Lesiok's option is probably the least effort. Here are some other options:
Create a slot for each step. In the slot for step 1 start the relevant process and connect() its finished() signal to the slot for step 2... and so on. You should check the exit state of step
n at the top of step
n+1 before starting the new step's process.
Or, between steps:
Code:
connect(process, SIGNAL(finished(...)), &loop, SLOT(quit()));
loop.exec();
The problem here, is you are assuming that the QProcess is still running when you start the event loop.
With the following code
Code:
connect(process, SIGNAL(finished(...)), &loop, SLOT(quit()));
process.start();
loop.exec()
its possible for short running executables that the process ends before the loop.exec command. This has bit me in the butt more than once.
The best solution I have come up with is the following
Code:
bool processFinished = false;
connect(process, &QProcess::finished,
[&loop, &processFinished]()
{
processFinished = true;
if ( loop.isRunning() )
loop.exit();
}
)
process.start();
loop.exec()