Made the code more robust.
Extended functionality with helper functions.
This commit is contained in:
49
bin/app.php
49
bin/app.php
@@ -3,33 +3,42 @@
|
||||
|
||||
require_once __DIR__ . '/../vendor/autoload.php';
|
||||
|
||||
use function Joop\Asynchronous\async;
|
||||
use Joop\Asynchronous\Promise;
|
||||
|
||||
// Create a function we want to run asynchronously
|
||||
$process = function ($i) {
|
||||
$delayMicroseconds = (10 - $i) * 1000000;
|
||||
usleep($delayMicroseconds);
|
||||
|
||||
return getmypid();
|
||||
};
|
||||
|
||||
|
||||
// Execute the functions asynchronously - each returning a Promise
|
||||
$promises = [];
|
||||
foreach (range(0, 10) as $i)
|
||||
$promises[] = Asynchronous::run($process, $i);
|
||||
|
||||
|
||||
// Wait for all promises to resolve
|
||||
while (count($promises) > 0) {
|
||||
/**
|
||||
* @param Promise[] $promises
|
||||
*/
|
||||
function awaitPromises(array &$promises)
|
||||
{
|
||||
foreach ($promises as $index => $promise) {
|
||||
if ($promise->isResolved() && !$promise->isEmpty()) {
|
||||
print("Response retrieved: " . $promise->getValue() . PHP_EOL);
|
||||
if ($promise->isResolved()) {
|
||||
unset($promises[$index]);
|
||||
|
||||
if (!$promise->isEmpty() && !$promise->isError())
|
||||
print($promise->getValue() . PHP_EOL);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
exit(0);
|
||||
/*
|
||||
* Example of asynchronous processing in PHP
|
||||
*/
|
||||
|
||||
$process = function ($i) {
|
||||
$delayMicroseconds = (5 - $i) * 1000000;
|
||||
usleep($delayMicroseconds);
|
||||
|
||||
return sprintf(
|
||||
'PID %-5d slept for %.1f seconds',
|
||||
getmypid(), $delayMicroseconds / 1000000
|
||||
);
|
||||
};
|
||||
|
||||
$promises = [];
|
||||
foreach (range(0, 5) as $i)
|
||||
$promises[] = async($process, $i);
|
||||
|
||||
while (count($promises) > 0)
|
||||
awaitPromises($promises);
|
||||
|
||||
15
bin/example/arrays.php
Executable file
15
bin/example/arrays.php
Executable file
@@ -0,0 +1,15 @@
|
||||
#!/usr/bin/php
|
||||
<?php
|
||||
|
||||
use function Joop\Asynchronous\async;
|
||||
|
||||
require_once __DIR__ . '/../../vendor/autoload.php';
|
||||
|
||||
$promise = async(function () {
|
||||
sleep(1);
|
||||
|
||||
return range(random_int(0, 10), random_int(20, 60));
|
||||
});
|
||||
|
||||
$array = $promise->resolve()->getValue();
|
||||
var_dump($array);
|
||||
37
bin/example/objects.php
Executable file
37
bin/example/objects.php
Executable file
@@ -0,0 +1,37 @@
|
||||
#!/usr/bin/php
|
||||
<?php
|
||||
|
||||
use function Joop\Asynchronous\async;
|
||||
|
||||
require_once __DIR__ . '/../../vendor/autoload.php';
|
||||
|
||||
|
||||
// Example class
|
||||
class Sample
|
||||
{
|
||||
private $data;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->data = [1, 2, 3];
|
||||
}
|
||||
|
||||
public function getData()
|
||||
{
|
||||
return $this->data;
|
||||
}
|
||||
}
|
||||
|
||||
// Create the process
|
||||
$promise = async(function () {
|
||||
sleep(2);
|
||||
|
||||
return new Sample();
|
||||
});
|
||||
|
||||
// We can do some other stuff here while the process is running
|
||||
|
||||
// Resolve the promise
|
||||
/** @var Sample $sample */
|
||||
$sample = $promise->resolve()->getValue();
|
||||
var_dump($sample->getData());
|
||||
14
bin/example/sample.php
Executable file
14
bin/example/sample.php
Executable file
@@ -0,0 +1,14 @@
|
||||
#!/usr/bin/php
|
||||
<?php
|
||||
|
||||
use function Joop\Asynchronous\async;
|
||||
|
||||
require_once __DIR__ . '/../../vendor/autoload.php';
|
||||
|
||||
$process = function ($number) {
|
||||
sleep($number);
|
||||
return $number;
|
||||
};
|
||||
|
||||
async($process, 2);
|
||||
// Do stuff...
|
||||
Reference in New Issue
Block a user