Amp v3 is a PHP async framework built on Fibers. It provides Future, async(), delay(), structured concurrency, and cancellation. This lab covers the Amp event loop, parallel task execution, async HTTP, and cancellable operations.
💡 With delay(0.02) per task sequentially = 100ms. Concurrently = ~20ms. That's the power of async!
Step 3: Future Combinators
Step 4: Coroutines with Fiber
Step 5: Async Simulation — Parallel Tasks
📸 Verified Output:
Step 6: Cancellation
Step 7: amphp/http-client — Async HTTP
💡 Install amphp/http-client separately. The above requires internet access in the Docker container. For offline demos, use simulateApiCall() with delay().
<?php
require 'vendor/autoload.php';
use function Amp\async;
use function Amp\delay;
// Amp v3 coroutines are plain PHP functions using async/await
// Under the hood: each async() call creates a Fiber
async(function(): void {
echo "Coroutine A: start\n";
delay(0.01);
echo "Coroutine A: after first delay\n";
delay(0.01);
echo "Coroutine A: done\n";
});
async(function(): void {
echo "Coroutine B: start\n";
delay(0.005);
echo "Coroutine B: after delay\n";
delay(0.015);
echo "Coroutine B: done\n";
});
// Wait for everything to complete
\Amp\delay(0.05); // give both time to finish
echo "Main: all coroutines done\n";