1. Go to this page and download the library: Download amphp/sync library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
use Amp\Future;
use Amp\Sync\LocalMutex;
use Amp\Sync\LocalParcel;
use function Amp\async;
use function Amp\delay;
$parcel = new LocalParcel(new LocalMutex(), 42);
$future1 = async(function () use ($parcel): void {
echo "Coroutine 1 started\n";
$result = $parcel->synchronized(function (int $value): int {
delay(1); // Delay for 1s to simulate I/O.
return $value * 2;
});
echo "Value after access in coroutine 1: ", $result, "\n";
});
$future2 = async(function () use ($parcel): void {
echo "Coroutine 2 started\n";
$result = $parcel->synchronized(function (int $value): int {
delay(1); // Delay again in this coroutine.
return $value + 8;
});
echo "Value after access in coroutine 2: ", $result, "\n";
});
Future\await([$future1, $future2]); // Wait until both coroutines complete.
use Amp\Future;
use function Amp\async;
use function Amp\delay;
[$left, $right] = createChannelPair();
$future1 = async(function () use ($left): void {
echo "Coroutine 1 started\n";
delay(1); // Delay to simulate I/O.
$left->send(42);
$received = $left->receive();
echo "Received ", $received, " in coroutine 1\n";
});
$future2 = async(function () use ($right): void {
echo "Coroutine 2 started\n";
$received = $right->receive();
echo "Received ", $received, " in coroutine 2\n";
delay(1); // Delay to simulate I/O.
$right->send($received * 2);
});
Future\await([$future1, $future2]); // Wait until both coroutines complete.
use Amp\Pipeline\Pipeline;
use function Amp\delay;
$urls = [...];
$results = Pipeline::fromIterable($urls)
->concurrent(10) // Process up to 10 URLs concurrently
->unordered() // Results may arrive out of order
->map(fetch(...)) // Map each URL to fetch(...)
->toArray();
var_dump($results);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.