How can I run 3 QProcess in background using the same executable



  • I have a program that has a Worker class and this class has 3 QProcesses.

    Each process will require a QDate as an argument. I want to run all of them in background.

    The Problem

    If I try to specify more than 3 dates (meaning all processes are running), the entire program freezes.

    I've already tried using QProcess::startDetached but it pops up command prompts which I don't like.

    I want everything to run in background, 3 processes running simultaneously.

    Here's my code:

    in

    I have a program that has a Worker class and this class has 3 QProcesses.

    Each process will require a QDate as an argument. I want to run all of them in background.

    The Problem

    If I try to specify more than 3 dates (meaning all processes are running), the entire program freezes.

    I've already tried using QProcess::startDetached but it pops up command prompts which I don't like.

    I want everything to run in background, 3 processes running simultaneously.

    Here's my code:

    in Worker::Worker()

    worker1 = new QProcess(this);
    worker2 = new QProcess(this);
    worker3 = new QProcess(this);
    
    connect(worker1, SIGNAL(finished(int,QProcess::ExitStatus)),
            this, SLOT(onFinished1(int,QProcess::ExitStatus)));
    connect(worker1, SIGNAL(error(QProcess::ProcessError)),
            this, SLOT(onError1(QProcess::ProcessError)));
    
    connect(worker2, SIGNAL(finished(int,QProcess::ExitStatus)),
            this, SLOT(onFinished2(int,QProcess::ExitStatus)));
    connect(worker2, SIGNAL(error(QProcess::ProcessError)),
            this, SLOT(onError2(QProcess::ProcessError)));
    
    connect(worker3, SIGNAL(finished(int,QProcess::ExitStatus)),
            this, SLOT(onFinished3(int,QProcess::ExitStatus)));
    connect(worker3, SIGNAL(error(QProcess::ProcessError)),
            this, SLOT(onError3(QProcess::ProcessError)));
    

    in Worker::run()

    while(start <= end) {
        date_list.push_back(start); // enqueue to QQueue<QDate>
        start = start.addDays(1);
    }
    
    QProcess* current;
    QDate processed_date;
    QStringList args;
    
    while(1) {
        if (date_list.empty()) {
           // Done processing all dates
           break;
        }
    
        bool has_available = false;
        if (worker1->state() != QProcess::Running) {
            current = worker1;
            processed_date = date_list.dequeue();
            has_available = true;
        } else if (worker2->state() != QProcess::Running) {
            current = worker2;
            processed_date = date_list.dequeue();
            has_available = true;
        } else if (worker3->state() != QProcess::Running) {
            current = worker3;
            processed_date = date_list.dequeue();
            has_available = true;
        }
    
        // Has available worker, start worker
        if (has_available) {
            QString sdate = processed_date.toString("yyyy-MM-dd");
            args << sdate;
            current->start(".\\program.exe", args);
            args.clear();
            emit processed(sdate);
        }
    }
    
    
    
    Any ideas?

  • Lifetime Qt Champion

    Hi,

    Yes, you're blocking everything with your infinite loop. You should refactor that to make use of Qt's asynchronous nature i.e. start each of your QProcesse and once one is done, restart with the next job etc. and once all is done, exit your thread if needed.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.