Boost PHP API Calls with Async and Parallel Execution Using Spatie/Async
When processing large volumes of data that require remote API calls, serial execution can cause severe latency, but by applying asynchronous calls and parallel processing with the Spatie/Async PHP library, you can cut total runtime by more than half while handling errors gracefully.
Scenario
When handling a large amount of data that requires remote API calls, serial execution becomes a bottleneck: each call takes about one second, so ten calls take ten seconds plus network latency, and any failure multiplies the total time, reducing responsiveness and stability.
To address this, we can use two optimization strategies:
Asynchronous execution : invoke remote APIs asynchronously so the program can continue other work while waiting for responses.
Parallel processing : use multithreading or multiprocessing to launch multiple API calls simultaneously, dramatically reducing overall duration.
Existing Solution
Remote API Example
Assume a third‑party endpoint is implemented as follows:
<?php
public function sync(): \support\Response {
sleep(1);
return json(['data' => date('Y-m-d H:i:s')]);
}Endpoint URL: http://127.0.0.1:8888/index/sync
Business System Example
The calling code loops ten times and fetches the endpoint synchronously:
<?php
declare(strict_types=1);
foreach (range(1, 10) as $key) {
$list[] = file_get_contents("http://127.0.0.1:8888/index/sync");
}
print_r($list);Output shows the total execution time of 10.14 seconds.
Asynchronous Parallel Calls
The spatie/async library provides a lightweight wrapper around PHP's PCNTL extension, allowing you to run processes in parallel with a simple API. Official repository:
https://github.com/spatie/asyncInstallation
Install via Composer:
composer require spatie/asyncNote: The library requires the pcntl and posix extensions. If they are missing, the Pool class will fall back to synchronous execution.
You can check platform support with:
require '../vendor/autoload.php';
use Spatie\Async\Pool;
var_dump(Pool::isSupported());It prints true if async processes are supported, otherwise false .
Usage
<?php
/**
* @author Tinywan (ShaoBo Wan)
* @date 2024/5/16
*/
declare(strict_types=1);
require '../vendor/autoload.php';
use Spatie\Async\Pool;
$timeOne = microtime(true);
$pool = Pool::create();
foreach (range(1, 10) as $item) {
$pool[] = async(function () use ($item) {
return file_get_contents("http://127.0.0.1:8888/index/sync");
})->then(function (string $output) use (&$list) {
// Handle success
$list[] = $output;
})->catch(function (Throwable $exception) {
// Handle exception
echo '[x] [异常] ' . $exception->getMessage() . PHP_EOL;
});
}
await($pool);
$timeTwo = microtime(true);
echo '[x] [系统调用耗时时间] ' . ($timeTwo - $timeOne) . PHP_EOL;
print_r($list);Running the above yields a total execution time of 4.34 seconds, saving roughly half of the time compared to the serial approach.
This demonstrates how asynchronous and parallel execution with spatie/async can dramatically improve performance for bulk remote API calls in PHP.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Open Source Tech Hub
Sharing cutting-edge internet technologies and practical AI resources.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
