PHP code example of dan-rogers / threadpool

1. Go to this page and download the library: Download dan-rogers/threadpool library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

dan-rogers / threadpool example snippets



// Get info by ID
$client = new HttpClient();
$info = $client->getInfo($argv[1]);

// Run some processing
$processed = $this->processInfo($info);

// Dispatch to service
$client->post($processed);


use Psr\Log\NullLogger;
use WorkerPool\WorkerPool;

$account_ids = [1, 2, 3, 4 ... 199, 200];

$base_command = 'php child-process.php'; // The child process we want to execute.
$logger = new NullLogger(); // A PSR 3 compliant logger to be used for monitoring and logging.
$max_threads = 10; // The max number of threads the workerpool can utilise at once.
$thread_check_sleep = 3000; // When all threads are in use how long should the pool wait before checking for an available thread.
$max_event_exec_time = 60000; // The maximum amount of time the thread should live for.
$check_limit = 1000; // How many checks should the pool make for a free thread before exiting.
$output_destination = 'mylog.log'; // Where the output of the child should be directed.

$pool = new WorkerPool(
    $base_command,
    $logger,
    $max_threads,
    $thread_check_sleep,
    $max_event_exec_time,
    $check_limit,
    $output_destination
);

foreach ($account_ids as $id) {
    // You can alternatively use WorkerPool::setJobs() to set all at once
    $pool->pushJob[$id]);
}

$pool->execute();