PHP Classes

PHP Async Executor: Run PHP scripts in parallel using the CLI version

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 147 All time: 9,123 This week: 62Up
Version License PHP version Categories
asyncexecutor 7GNU General Publi...5Tools, Files and Folders, Unix, Console, L..., P...
Description 

Author

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

It can run a given PHP script by executing the PHP CLI version. It passes the name of the script and the parameter values as command line parameter values.

The package may start a process to run an existing script file. It may also wait a given time if the script file does not exist, allowing another process to create the script file while the package waits.

Innovation Award
PHP Programming Innovation award nominee
June 2021
Number 6
One way to run multiple PHP scripts simultaneously is to use the PHP CLI version and start it as a parallel process. The start is a bit slow because a new program needs to be loaded.

To start faster, this package provides a solution that can start the parallel process first before creating the actual script that the process will run.

Manuel Lemos
Picture of Alexandre Sinício
  Performance   Level  
Name: Alexandre Sinício <contact>
Classes: 7 packages by
Country: Brazil Brazil
Age: 40
All time rank: 2095136 in Brazil Brazil
Week rank: 47 Up3 in Brazil Brazil Up
Innovation award
Innovation award
Nominee: 3x

Example

<?php
use alesinicio\AsyncExecutor\AsyncExecutor;

require
__DIR__.'/../vendor/autoload.php';

//INITIALIZE THE EXECUTOR
$async = new AsyncExecutor('/usr/bin/php');

//RUN THE PROCESS
$pid = $async->runProcess(__DIR__.'/bg_process.php', ['param01', 'param02']);

//CHECK IF PROCESS IS RUNNING BY CHECKING ITS PID
var_dump($async->isProcessRunning($pid));


Details

Async Processor

This a simple class that can help spawning CLI processes, either as "run-and-forget" or "run-and-keep-running" modes.

Usage

Run-and-forget

Useful when you want to spawn a CLI process and make it run in the background. You will NOT get any return from the process itself.

Basic usage:

$async = new AsyncExecutor('/usr/bin/php');
$async->runProcess('path_to_script', ['param01', 'param02']);

Run-and-keep-running

Useful when you want to spawn CLI processes and keep them running no matter what.

The class monitors the PID of the processes to check if they are still running, and restarts automatically if needed.

Basic usage:

$async = new AsyncExecutor('/usr/bin/php');
$multiAsync = new AsyncMultiProcess($async);
$multiAsync->addProcess(new AsyncProcess('instance_01', 'path_to_script_01', ['param01']));
$multiAsync->addProcess(new AsyncProcess('instance_02', 'path_to_script_02', ['param02', 'param03']));
$multiAsync->keepRunningProcesses();

If a process fails to execute due to a non-existing script, you can set the time the class will wait to retry the execution (default, 5 seconds).

You can also configure the AsyncMultiProcess to abort execution if non-existing script is detected, effectively canceling everything (running processes will not be closed, though).


  Files folder image Files (10)  
File Role Description
Files folder imageexamples (3 files)
Files folder imagesrc (4 files)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files (10)  /  examples  
File Role Description
  Accessible without login Plain text file bg_process.php Aux. Auxiliary script
  Accessible without login Plain text file run_and_forget.php Example Example script
  Accessible without login Plain text file run_and_keep_running.php Example Example script

  Files folder image Files (10)  /  src  
File Role Description
  Plain text file AsyncExecutor.php Class Class source
  Plain text file AsyncMultiProcess.php Class Class source
  Plain text file AsyncProcess.php Class Class source
  Plain text file FileNotFoundException.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 Download Rankings  
 100%
Total:147
This week:0
All time:9,123
This week:62Up