Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How can I run 3 QProcess in background using the same executable
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 632 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    josephdp
    wrote on last edited by
    #1

    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?
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      2

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved