Worker Pools in PHP

Our example demonstrates how to implement a worker pool using PHP’s multi-threading capabilities. We’ll use the pthreads extension, which allows for true parallel execution in PHP.

First, let’s define our worker class:

<?php

class Worker extends Thread {
    private $id;
    private $jobs;
    private $results;

    public function __construct($id, $jobs, $results) {
        $this->id = $id;
        $this->jobs = $jobs;
        $this->results = $results;
    }

    public function run() {
        while (!$this->jobs->isEmpty()) {
            $job = $this->jobs->shift();
            echo "worker {$this->id} started  job {$job}\n";
            sleep(1); // Simulate an expensive task
            echo "worker {$this->id} finished job {$job}\n";
            $this->results[] = $job * 2;
        }
    }
}

Now, let’s implement our main script:

<?php

$numJobs = 5;
$jobs = new Threaded();
$results = new Threaded();

// Create jobs
for ($j = 1; $j <= $numJobs; $j++) {
    $jobs[] = $j;
}

// Start workers
$workers = [];
for ($w = 1; $w <= 3; $w++) {
    $worker = new Worker($w, $jobs, $results);
    $worker->start();
    $workers[] = $worker;
}

// Wait for all workers to finish
foreach ($workers as $worker) {
    $worker->join();
}

// Print results
foreach ($results as $result) {
    echo "Result: $result\n";
}

In this example, we create a Worker class that extends the Thread class provided by pthreads. Each worker runs in its own thread and processes jobs from a shared job queue.

In the main script, we:

  1. Create a pool of jobs.
  2. Start three worker threads.
  3. Wait for all workers to finish.
  4. Print the results.

To run this program, you need to have the pthreads extension installed and enabled in your PHP environment. Save the code in a file named worker_pools.php and run it using:

$ php worker_pools.php
worker 1 started  job 1
worker 2 started  job 2
worker 3 started  job 3
worker 1 finished job 1
worker 1 started  job 4
worker 2 finished job 2
worker 2 started  job 5
worker 3 finished job 3
worker 1 finished job 4
worker 2 finished job 5
Result: 2
Result: 4
Result: 6
Result: 8
Result: 10

The program demonstrates concurrent execution of jobs by multiple workers. Despite having 5 seconds of total work (5 jobs, each taking 1 second), the program completes in about 2 seconds because the work is distributed among 3 workers running concurrently.

Note that PHP’s threading model with pthreads is different from some other languages. It’s important to properly manage shared resources and be aware of potential issues like race conditions when working with threads in PHP.