Pokio: A Dead Simple Asynchronous API for PHP
If you've ever wished PHP had a straightforward way to handle async tasks without diving into complex event loops or external dependencies, Pokio might be exactly what you're looking for. Created by Nuno Maduro, this lightweight library brings a clean, promise-based async API to PHP—with minimal fuss.
What It Does
Pokio provides a simple way to run tasks concurrently in PHP using promises. Under the hood, it leverages PCNTL (for process forking) and FFI (for shared memory communication) to execute closures in parallel. If those extensions aren’t available, it gracefully falls back to sequential execution—meaning your code won’t break, just run slower.
Here’s the gist:
$promiseA = async(function () {
sleep(2);
return 'Task 1';
});
$promiseB = async(function () {
sleep(2);
return 'Task 2';
});
// Resolves in ~2 seconds instead of ~4
[$resA, $resB] = await([$promiseA, $promiseB]);
Why It’s Cool
- No Boilerplate: Unlike heavier async solutions, Pokio’s API is minimal—just
async()andawait(). - Process-Based Parallelism: Uses PHP’s
pcntlto fork processes, avoiding the complexity of event loops. - Graceful Degradation: Works even without
pcntlorffi(just without parallelism). - Promise Chaining: Supports
then,catch, andfinallyfor composable async workflows.
It’s not a full-blown async framework, but for speeding up blocking operations (like multiple HTTP requests or file operations), it’s a clever, lightweight tool.
How to Try It
- Install (PHP 8.3+ required):
composer require nunomaduro/pokio:^0.1 - Use it:
use function Pokio\{async, await}; $promise = async(fn() => file_get_contents('https://example.com')); $response = await($promise); - Check the docs: GitHub repo for advanced usage.
Final Thoughts
Pokio isn’t for every use case (the author explicitly warns against production use due to its experimental nature), but it’s a fascinating experiment in bringing simple async patterns to PHP. If you’re optimizing a CLI tool or need lightweight parallelism, give it a spin. For production, you might still reach for ReactPHP or Amp—but Pokio is a refreshingly simple alternative for the right scenarios.
Would you use this in a project? Let me know on Twitter! 🚀