PHP Classes

How Can PHP Run Multiple Functions in Parallel Using the Package Generic Executor Service in PHP: Execute multiple functions in parallel

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
2024-11-06 (1 month ago) RSS 2.0 feedNot enough user ratingsTotal: 32 This week: 1All time: 11,101 This week: 42Up
Version License PHP version Categories
generic-executor-ser 1.0GNU General Publi...8Unix, Language, Performance and optim..., P...
Description 

Author

This package can execute multiple functions in parallel asynchronously.

It can take one or more callback PHP functions and execute the functions' code in parallel asynchronously using the pcntl extension.

It limits the number of concurrent workers (processes) executing the tasks, resulting in better resource management. The number of worker processes can also be limited to avoid using too much memory and CPU.

The package can wait for all functions to be executed in parallel to finish their execution.

Innovation Award
PHP Programming Innovation award nominee
November 2024
Nominee
Vote
Some PHP applications require executing many tasks, such as processing queues of information stored in many database table records.

One way to execute many tasks faster is to create parallel processes to execute the tasks.

This package can use the pcntl function to create parallel processes to execute many tasks as soon as possible without using too much memory or CPU by limiting the number of processes that are started.

Manuel Lemos
Picture of Nitesh Apte
  Performance   Level  
Name: Nitesh Apte is available for providing paid consulting. Contact Nitesh Apte .
Classes: 19 packages by
Country: India India
Age: 40
All time rank: 1317 in India India
Week rank: 5 Up1 in India India Up
Innovation award
Innovation award
Nominee: 5x

Winner: 1x

Instructions

Follow example code provided in example.php file.

Example

<?php
include 'GenericExecutorService.php';
// List of URLs to call
$urls = [
   
"https://jsonplaceholder.typicode.com/posts/1",
   
"https://jsonplaceholder.typicode.com/posts/2",
   
"https://jsonplaceholder.typicode.com/posts/3",
   
"https://jsonplaceholder.typicode.com/posts/4",
   
"https://jsonplaceholder.typicode.com/posts/5",
   
"https://jsonplaceholder.typicode.com/posts/6",
];

// Instantiate the service with a max of 3 workers
$executor = new GenericExecutorService(3);

foreach (
$urls as $url) {
   
$executor->submit(function() use ($url) {
       
// Each task fetches the URL content
       
$content = file_get_contents($url);
        echo
"Fetched from $url: " . substr($content, 0, 100) . "\n"; // Display a snippet of the response
   
});
}

// Wait for all workers to complete
$executor->shutdown();
?>


Details

Generic Executor Service in PHP

A Generic Executor Service in PHP for asynchronous calls. This repository demonstrates a basic implementation of a Generic Executor Service in PHP 8 using the `pcntl` extension to handle concurrency. It mimics the behavior of Java's ExecutorService by managing a pool of "workers" (processes) that execute tasks asynchronously.

generic-executor-service-php a language port from Java to PHP from the repository which I created a while ago called generic-executor-service-java (https://github.com/niteshapte/generic-executor-service-java)

Features

  • Task Submission: Submit tasks (callables) to be executed asynchronously by the worker pool.
  • Worker Pool Management: Limit the number of concurrent workers (processes) executing tasks.
  • Graceful Shutdown: Waits for all tasks to complete before shutting down.

Requirements

  • PHP 8.0+
  • 
    

Installation

Clone the repository:

git clone https://github.com/your-username/generic-executor-service-php.git
cd generic-executor-service-php

Ensure you have `pcntl` enabled in your PHP environment.

Run the script using the PHP CLI:

php executor.php

Handling Concurrency with `pcntl`

The `GenericExecutorService` relies on the `pcntl_fork()` function to create separate processes for each task. This provides concurrency in environments where PHP doesn't natively support multi-threading. However, `pcntl` is only available in CLI and POSIX-compliant environments (Linux/macOS).

Compatibility



## Customization
You can modify the following aspects of the service:
- Max Workers: Control the maximum number of concurrent workers by passing an integer to the GenericExecutorService constructor.
- Task Length: Simulate longer or shorter tasks by adjusting the sleep duration inside the tasks.


## Contribution
Contributions are welcome! If you'd like to improve this project, feel free to fork the repository and submit a pull request.


### Steps to Contribute:
1. Fork this repository by clicking the Fork button at the top right.
2. Clone your fork locally:

git clone https://github.com/your-username/generic-executor-service-php.git

3. Create a new branch for your feature or fix:

git checkout -b feature-branch

4. Make your changes and commit them:

git commit -m "Description of your changes"

5. Push to your fork:

git push origin feature-branch

6. Open a pull request on the original repository.


Feel free to suggest improvements, refactorings, or new features! All contributions, no matter how small, are greatly appreciated.

  Files folder image Files (4)  
File Role Description
Accessible without login Plain text file example.php Example Example script
Plain text file GenericExecutorService.php Class Class source
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file README.md Doc. Documentation

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:32
This week:1
All time:11,101
This week:42Up