PHP Classes

File: example.php

Recommend this page to a friend!
  Classes of Nitesh Apte   Generic Executor Service in PHP   example.php   Download  
File: example.php
Role: Example script
Content type: text/plain
Description: Example script
Class: Generic Executor Service in PHP
Execute multiple functions in parallel
Author: By
Last change: Update example.php
Date: 6 days ago
Size: 830 bytes
 

Contents

Class file image Download
<?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();
?>