Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
panosru opened this issue Apr 19, 2018 · 1 comment
Open

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

panosru opened this issue Apr 19, 2018 · 1 comment

Comments

@panosru
Copy link

panosru commented Apr 19, 2018

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!

@panosru
Copy link
Author

panosru commented Apr 19, 2018

Doing some more testing, I understand that the whole point is for background processing and seems to work just fine, but on my previous example, I mostly wondering if is possible to get an async output from the closures that are done first.

For instance the following code works just as expected:

<?php
declare(strict_types=1);

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

$manager = new \Spork\ProcessManager();

$path = __DIR__ . '/test.log';

register_shutdown_function(function () {
    echo "Okay I'm done! time: " . (new DateTime())->format('i:s') . PHP_EOL;
});

$processes = [
    function () use ($path) {
        sleep(3);
        $fileContents = file_get_contents($path);
        file_put_contents($path, $fileContents . "I am slow" . PHP_EOL);
    },
    function () use ($path) {
        sleep(5);
        $fileContents = file_get_contents($path);
        file_put_contents($path, $fileContents . "Nope, I'm slower!" . PHP_EOL);
    },
    function () use ($path) {
        $fileContents = file_get_contents($path);
        file_put_contents($path, $fileContents . "I'm really fast!" . PHP_EOL);
    },
    function () use ($path) {
        sleep(1);
        $fileContents = file_get_contents($path);
        file_put_contents($path, $fileContents . "I have to train more!" . PHP_EOL);
    }
];

echo "Before Process time: " . (new DateTime())->format('i:s') . PHP_EOL;

$manager->process(new RecursiveArrayIterator($processes), function ($process) {
    $process();
});

echo "After Process time: " . (new DateTime())->format('i:s') . PHP_EOL;

Running $ php fork/test.php I get the following output in the console:

Before Process time: 46:33
After Process time: 46:33
Okay I'm done! time: 46:33
Okay I'm done! time: 46:34
Okay I'm done! time: 46:41
Okay I'm done! time: 46:41

and the test.log file has the following content:

I'm really fast!
I have to train more!
I am slow
Nope, I'm slower!

That behaviour is the expected one, but I was wondering if is possible to have in the output of the console the "tasks" that are done first in time.

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant