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 (Yesterday) RSS 2.0 feedNot yet rated by the usersTotal: 1 This week: 1All time: 11,480 This week: 63Up
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.

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

It mimics the behavior of Java's ExecutorService by managing a pool of "workers" (processes) that execute tasks asynchronously.

The number of worker processes can 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.

This package is a PHP port from a similar package written in Java.

Picture of Nitesh Apte
  Performance   Level  
Name: Nitesh Apte <contact>
Classes: 18 packages by
Country: India India
Innovation award
Innovation award
Nominee: 4x

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:1
This week:1
All time:11,480
This week:63Up