hoaproject/Console

[Question] How to run a Hoa\Console\Processus in the background or in non blocking mode

shulard opened this issue · 4 comments

Hello !

I'm currently trying to build a worker launcher. I already run the process using something like :

$processus = new Processus(
    $script,
    $options
);
$processus->run();

The latest instruction block execution. I want to be able to :

  • run multiple processus in a loop ;
  • then listen event on each ;
  • know when execution is done.

Do you think it's possible with Hoa\Console package ? Maybe I lack some knowledge around process execution 😄

Thanks for your help !

Hywan commented

If you run the processus in the background with & (like sleep 2&), then it will run in the background I suppose. I didn't try it yet. However, it might not be compatible with DOS (it could with PowerShell though).

Thanks for your comment, I though it was a background problem but it's not... I discovered today that "Processus" is non blocking by default. I tried to perform a parallel worker run but can get the output result only on the first one...

An example of what I want to achieve here :

$procs = [];
$count = 10;
$script = "date";
while(--$count > 0)
{
    $proc = new Processus(
        $script,
        null,
        [1 => ['file', 'php://stdout', 'a']]
    );
    $proc->open();
    $procs[] = $proc;
}

while(true) {
    foreach($procs as $key => $proc) {
        $status = $proc->getStatus();
        if(false === $status['running']) {
            finishProcess($proc, $status);
            unset($procs[$key]);
        }
    }

    if(0 === count($procs)) {
        break;
    }
}

I ran these 10 scripts without errors but I got the same PID for each using $proc->getStatus() and a -1 exitcode for all except the first... The 10 process gave me running===true inside the loop...

Is it possible to run multiple proc_open processes in parallel in the same script ?

Hywan commented

Yes, by using & but this is not native to Hoa\Console\Processus. You could open an issue for this new feature.

I've open an issue for the specific multi process feature: #73