PHP Classes

How to Implement the PHP Await Function with the pcntl Extension using the Package Task PHP: Run in parallel PHP code tasks and await for them

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2026-01-29 (4 months ago) RSS 2.0 feedNot yet rated by the usersTotal: Not yet counted Not yet ranked
Version License PHP version Categories
taskphp 1.0MIT/X Consortium ...7Unix, Performance and optimization, P...
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
PHP Programming Innovation award nominee
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
Picture of Nahid Bin Azhar
  Performance   Level  
Name: Nahid Bin Azhar <contact>
Classes: 27 packages by
Country: Bangladesh Bangladesh
Age: 35
All time rank: 788
Week rank: 5 Up
Innovation award
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


  Files folder image Files (31)  
File Role Description
Files folder imageexamples (3 files)
Files folder imagesrc (3 files, 6 directories)
Files folder imagetests (2 directories)
Accessible without login Plain text file .phpunit.result.cache Data Auxiliary data
Accessible without login Plain text file AGENT_INSTRUCTIONS.md Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Plain text file FRAMEWORK_INTEGRATION.md Class Class source
Accessible without login Plain text file phpunit.xml Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation
Plain text file verify_bootstrap.php Class Class source
Accessible without login Plain text file verify_bootstrap_real.php Example Example script

  Files folder image Files (31)  /  examples  
File Role Description
  Accessible without login Plain text file background.log Data Auxiliary data
  Plain text file bootstrap_example.php Class Class source
  Accessible without login Plain text file demo.php Example Example script

  Files folder image Files (31)  /  src  
File Role Description
Files folder imagebin (1 file)
Files folder imageBootstrap (3 files)
Files folder imageContracts (3 files)
Files folder imageExceptions (3 files)
Files folder imageIPC (2 files)
Files folder imageProcess (3 files)
  Plain text file CallbackTask.php Class Class source
  Plain text file Task.php Class Class source
  Plain text file TaskGroup.php Class Class source

  Files folder image Files (31)  /  src  /  bin  
File Role Description
  Accessible without login Plain text file worker.php Example Example script

  Files folder image Files (31)  /  src  /  Bootstrap  
File Role Description
  Plain text file AbstractBootstrap.php Class Class source
  Plain text file LaravelBootstrap.php Class Class source
  Plain text file WordPressBootstrap.php Class Class source

  Files folder image Files (31)  /  src  /  Contracts  
File Role Description
  Plain text file TaskBootstrapInterface.php Class Class source
  Plain text file TaskInterface.php Class Class source
  Plain text file TaskLifecycleInterface.php Class Class source

  Files folder image Files (31)  /  src  /  Exceptions  
File Role Description
  Plain text file TaskException.php Class Class source
  Plain text file TaskFailedException.php Class Class source
  Plain text file TimeoutException.php Class Class source

  Files folder image Files (31)  /  src  /  IPC  
File Role Description
  Plain text file Pipe.php Class Class source
  Plain text file Serializer.php Class Class source

  Files folder image Files (31)  /  src  /  Process  
File Role Description
  Plain text file ChildProcess.php Class Class source
  Plain text file ProcessManager.php Class Class source
  Plain text file ProcessResult.php Class Class source

  Files folder image Files (31)  /  tests  
File Role Description
Files folder imageIntegration (1 file)
Files folder imageUnit (1 file)

  Files folder image Files (31)  /  tests  /  Integration  
File Role Description
  Plain text file TaskTest.php Class Class source

  Files folder image Files (31)  /  tests  /  Unit  
File Role Description
  Plain text file SerializerTest.php Class Class source

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads  
 100%
Total:0
This week:0