You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
<?phpdeclare(strict_types=1);
require_oncedirname(__DIR__) . '/vendor/autoload.php';
$manager = new \Spork\ProcessManager();
$path = __DIR__ . '/test.log';
register_shutdown_function(function () {
echo"Okay I'm done! time: " . (newDateTime())->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: " . (newDateTime())->format('i:s') . PHP_EOL;
$manager->process(newRecursiveArrayIterator($processes), function ($process) {
$process();
});
echo"After Process time: " . (newDateTime())->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.
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:
when I run
$ php fork/test.php
, I get the following output:shouldn't instead of getting the following output?
Am I missing something? should I use the
$manager->wait()
method instead ofsleep
instead?Thanks!
The text was updated successfully, but these errors were encountered: