hoaproject / Console

The Hoa\Console library.
https://hoa-project.net/
366 stars 32 forks source link

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

Closed shulard closed 8 years ago

shulard commented 8 years ago

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 :

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

Thanks for your help !

Hywan commented 8 years ago

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).

shulard commented 8 years ago

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 8 years ago

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

shulard commented 8 years ago

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