PHP Classes

How to Implement PHP Asynchronous Processing to Execute PHP Scripts and Closure Functions in Parallel Using the Package PHP Async: Run scripts in parallel using the PHP CLI command

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
2025-07-15 (2 days ago) RSS 2.0 feedNot yet rated by the usersTotal: Not yet counted Not yet ranked
Version License PHP version Categories
php-async 1.0Custom (specified...7Language, Performance and optimization, P...
Description 

Author

This package can run scripts in parallel using the PHP CLI command.

It provides a class that can start processes to run given PHP scripts in parallel using the PHP CLI command. It can run on UNIX based systems like Linux and Windows.

The package also provides a class that can also execute a given closure function in parallel, using shared memory to send the closure function to a parallel process that will execute the function.

Picture of Lucas Marques Dutra
  Performance   Level  
Name: Lucas Marques Dutra <contact>
Classes: 1 package by
Country: Brazil Brazil
Innovation award
Innovation award
Nominee: 1x

Instructions

Example

<?php

require_once 'vendor/autoload.php';

use
Terremoth\Async\Process;
use
Terremoth\Async\PhpFile;

$process = new Process();

echo
date('c') . ' :: Sending process. You should not wait any longer to see next message: ' . PHP_EOL;

try {
   
$age = 30;
   
$name = 'John Doe';
   
$fruits = ['orange', 'apple', 'grape'];
   
$process->send(function () use ($age, $name, $fruits) {
       
sleep(5);
        echo
123; // you should not see this anywhere
       
file_put_contents(
           
'demo.txt',
           
"Age: $age\nName: $name\nFruits: " . implode(', ', $fruits) . ' - ' . date('c')
        );
    });
} catch (
Exception $e) {
    echo
$e->getMessage();
}

echo
date('c') . ' :: This is the next message' . PHP_EOL;
echo
date('c') . ' :: Now let\'s process a file that takes a long time...' . PHP_EOL;

try {
   
$file = new PhpFile(__DIR__ . DIRECTORY_SEPARATOR . 'time-wasting-file.php');
   
$file->run();
    echo
date('c') . ' :: Ended...' . PHP_EOL;
} catch (
Exception $e) {
    echo
$e->getMessage();
}


Details

PHP Async Process

Process functions or files asynchronously without needing AMP, ReactPHP, RxPHP, Spatie/Fork, Fibers, Pthreads, Parallel, Revolt, Pcntl or Swoole.

Just raw PHP! It is magic!

<!-- codecov Test Coverage --> Psalm type coverage Psalm level Test Run Status Codacy Badge Maintainability License Packagist Downloads

It uses a combination of: - serializable-clojure lib - Symfony/Process lib - and PHP's native Shmop extension

Warning

it does not works on MSYS or MINGW terminals! However, It will work fine on both Windows (cmd and powershell) and Linux.

See demos/demo.php for examples.

Installation

composer require terremoth/php-async

Documentation

<?php

require_once 'vendor/autoload.php';

use Terremoth\Async\PhpFile;
use Terremoth\Async\Process;

$process = new Process();
$age = 30;
$name = 'John Doe';
$fruits = ['orange', 'apple', 'grape'];

$process->send(function () use ($age, $name, $fruits) {
    /*
    // Anything you want to process here, you can use closure vars
    // In a future version I will create communications variables between both processes
    */
});

// Another way to use is if you want to just process a file Asynchronously, you can do this:
$args = ['--verbose', '-n', '123'];
$asyncFile = new PhpFile('existing-php-file.php', $args); // make sure to pass the correct file with its path
$asyncFile->run();

That's it!


  Files folder image Files (17)  
File Role Description
Files folder image.github (1 directory)
Files folder imagedemos (2 files)
Files folder imagesrc (1 directory)
Files folder imagetests (1 directory)
Accessible without login Plain text file .editorconfig Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file composer.lock Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file phpcs.xml Data Auxiliary data
Accessible without login Plain text file phpmd.xml Data Auxiliary data
Accessible without login Plain text file phpunit.xml Data Auxiliary data
Accessible without login Plain text file psalm.xml Data Auxiliary data
Accessible without login Plain text file README.md Doc. Read me

  Files folder image Files (17)  /  .github  
File Role Description
Files folder imageworkflows (1 file)

  Files folder image Files (17)  /  .github  /  workflows  
File Role Description
  Accessible without login Plain text file workflow.yml Data Auxiliary data

  Files folder image Files (17)  /  demos  
File Role Description
  Accessible without login Plain text file demo.php Example Example script
  Accessible without login Plain text file time-wasting-file.php Aux. Configuration script

  Files folder image Files (17)  /  src  
File Role Description
Files folder imageTerremoth (1 directory)

  Files folder image Files (17)  /  src  /  Terremoth  
File Role Description
Files folder imageAsync (4 files)

  Files folder image Files (17)  /  src  /  Terremoth  /  Async  
File Role Description
  Accessible without login Plain text file background_processor.php Aux. Auxiliary script
  Plain text file PhpFile.php Class Class source
  Plain text file Process.php Class Class source
  Accessible without login Plain text file script_functions.php Aux. Configuration script

  Files folder image Files (17)  /  tests  
File Role Description
Files folder imageTerremoth (1 directory)

  Files folder image Files (17)  /  tests  /  Terremoth  
File Role Description
Files folder imageAsyncTest (1 file)

  Files folder image Files (17)  /  tests  /  Terremoth  /  AsyncTest  
File Role Description
  Plain text file PhpFileTest.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