Skip to content

Trying to figure out async processes but some help is needed :) #43

@panosru

Description

@panosru

Hello, not an issue, just a question I have in order to figure out few things with that library.

if I have the following code:

<?php
declare(strict_types=1);

require_once dirname(__DIR__) . '/vendor/autoload.php';

$manager = new \Spork\ProcessManager();

$processes = [
    function () {
        sleep(3);
        return "I am slow";
    },
    function () {
        sleep(5);
        return "Nope, I'm slower!";
    },
    function () {
        return "I'm really fast!";
    },
    function () {
        sleep(1);
        return "I have to train more!";
    }
];

foreach ($processes as $process) {
    $manager->fork($process)->done(function (\Spork\Fork $fork) {
        echo "PID: [{$fork->getPid()}] | {$fork->getResult()}\n";
    });
}

echo 'I am the flash!' . PHP_EOL;

when I run $ php fork/test.php, I get the following output:

I am the flash!
PID: [21444] | I am slow
PID: [21445] | Nope, I'm slower!
PID: [21446] | I'm really fast!
PID: [21447] | I have to train more!

shouldn't instead of getting the following output?

I am the flash!
PID: [21446] | I'm really fast!
PID: [21447] | I have to train more!
PID: [21444] | I am slow
PID: [21445] | Nope, I'm slower!

Am I missing something? should I use the $manager->wait() method instead of sleep instead?

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions