| Last Updated | | Ratings | | Unique User Downloads | | Download Rankings |
2026-01-29 (4 months ago)  | | Not yet rated by the users | | Total: Not yet counted | | Not yet ranked |
|
| Description | | Author |
This package can run in parallel PHP code tasks and await for them.
It provides a class that can take a list of one or more tasks to be executed using PHP code provide as callback functions.
The class can execute the code of the tasks in parallel using the pcntl
It also can await for the tasks to them to finish and return the results of each of the executed tasks in an array or the result of another callback function that processes the results of all the tasks.
The class can also execute the tasks and not wait for the tasks to be concluded. Innovation Award
 January 2026
Number 3 |
Some applications require executing tasks many times, like for instances sending an email newsletter for many users.
One way to run those tasks faster, is to start multiple processes to execute the tasks in parallel. The tasks may still take a long time to execute.
The await function is used by languages like JavaScript to defined what code will be executed when a task running asynchronously ends.
The current versions of PHP do not have a native function that works like the await function of JavaScript.
This package implements a way to execute many tasks asynchronously and execute a given PHP code after all tasks end, using a function that works like the await function.
Manuel Lemos |
| |
 |
|
Innovation award
 Nominee: 8x
Winner: 2x |
|
Instructions
Example
<?php
require __DIR__ . '/../vendor/autoload.php';
use Nahid\TaskPHP\Task;
// 1. Basic Async/Await
echo "--- Running tasks concurrently and awaiting results ---\n";
$vals = Task::async([
'val' => function () {
sleep(2);
return "5";
}
])->await();
print_r($vals);
// 2. Await with callback processing
echo "\n--- Processing results with a callback ---\n";
$sum = Task::async([
'a' => fn() => 10,
'b' => fn() => 20
])->await(fn($res) => array_sum($res));
echo "Sum: $sum\n";
// 3. Fire and Forget (Background)
echo "\n--- Dispatched background task. Check background.log in 2 seconds ---\n";
Task::async([
'bg' => function () {
sleep(2);
file_put_contents(__DIR__ . '/background.log', "Background job finished at " . date('H:i:s') . "\n");
}
])->forget();
echo "Main script exiting. Worker is still running in background!\n";
|
Details
TaskPHP
A framework-agnostic PHP concurrency library designed for high-performance task execution using child processes. It provides a modern, clean async/await pattern to manage parallel jobs without complex message brokers.
Installation
composer require nahid/task-php
Features
-
Async/Await Pattern: Start tasks and await results when needed.
-
Concurrency Control: Limit the number of parallel workers.
-
Timeouts: Automatic process termination for slow tasks.
-
Fail-Fast: Optional ability to cancel all tasks if one fails.
-
Framework Agnostic: Works standalone or with Laravel, WordPress, etc.
Basic Usage
1. Simple Async Execution
use Nahid\TaskPHP\Task;
$results = Task::async([
'task1' => fn() => 10,
'task2' => fn() => 20,
])->await();
// $results = ['task1' => 10, 'task2' => 20]
2. Await with Result Processing
$sum = Task::async([
'a' => fn() => 10,
'b' => fn() => 20
])->await(fn($res) => array_sum($res));
echo $sum; // 30
3. Fire and Forget (Background)
Task::async([
'send_email' => function() {
// This runs in the background
}
])->forget();
Configuration
$results = Task::limit(5) // Concurrent workers
->timeout(30) // Max execution time
->bootstrap('init.php') // Bootstrap framework
->failFast(true) // Stop all on first error
->async($tasks)
->await();
License
MIT (c) Nahid
| |
Applications that use this package |
|
No pages of applications that use this class were specified.
If you know an application of this package, send a message to the author to add a link here.