hoaproject / Console

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

Process: Async API #73

Open shulard opened 8 years ago

shulard commented 8 years ago

Hello !

As discussed in the #72, I create an issue for this new feature. The goal here is to use Process class to run multiple scripts from the same script.

Example code 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;
    }
}

For the moment the same PID is shared by all the Process instances. As @Hywan said in the mentioned issue, there is a solution by using &.

Thanks for your help !

--- Want to back this issue? **[Post a bounty on it!](https://www.bountysource.com/issues/37491583-process-async-api?utm_campaign=plugin&utm_content=tracker%2F459935&utm_medium=issues&utm_source=github)** We accept bounties via [Bountysource](https://www.bountysource.com/?utm_campaign=plugin&utm_content=tracker%2F459935&utm_medium=issues&utm_source=github).