PHPackages                             hdvianna/parallel-workerpool - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Queues &amp; Workers](/categories/queues)
4. /
5. hdvianna/parallel-workerpool

ActiveLibrary[Queues &amp; Workers](/categories/queues)

hdvianna/parallel-workerpool
============================

1.1.2(5y ago)88MITPHPPHP ^7.2CI failing

Since Jan 17Pushed 5y ago1 watchersCompare

[ Source](https://github.com/hdvianna/parallel-workerpool)[ Packagist](https://packagist.org/packages/hdvianna/parallel-workerpool)[ RSS](/packages/hdvianna-parallel-workerpool/feed)WikiDiscussions master Synced 3w ago

READMEChangelogDependenciesVersions (7)Used By (0)

Parallel Worker Pool
====================

[](#parallel-worker-pool)

Parallel worker pool uses the [PHP parallel extension](https://www.php.net/manual/en/book.parallel.php)to provide a simple interface for dealing with parallelization of tasks.

Usage
-----

[](#usage)

The `WorkerPool` requires an implementation of the `WorkFactoryInterface`which is responsible for creating the *consumer* and *producer closures*. A producer closure must return a [Generator](https://www.php.net/manual/en/class.generator.php).

### Composer installation

[](#composer-installation)

`composer require hdvianna/parallel-workerpool`

### Runing with Docker

[](#runing-with-docker)

`docker-compose up`

Docker compose builds an environment with the needed extensions installed and create a bind mount to the current directory.

### Example

[](#example)

In this example 10 workers will sleep for *n* milliseconds, each time they consume the work generated by the WorkFactory.

```
use hdvianna\Concurrent\WorkFactoryInterface;
use hdvianna\Concurrent\WorkerPool;

(new WorkerPool(new class implements WorkFactoryInterface {
    public function createWorkGeneratorClosure(): \Closure
    {
        return function () {
            for ($i = 0; $i < 100; $i++) {
                $work = new \stdClass();
                $work->time = mt_rand(300, 1000);
                $work->id = $i;
                yield $work;
            }
        };
    }

    public function createWorkConsumerClosure(): \Closure
    {
        return function($work) {
            printf("[$work->id]: Sleeping for %d milliseconds ...%s", $work->time, PHP_EOL);
            usleep($work->time * 1000);
            printf("[$work->id]: Woke up after %d milliseconds ...%s", $work->time, PHP_EOL);
        };
    }

}, 10))->run();
```

### Synchronizing data

[](#synchronizing-data)

Data can be synchronized by using lock and unlock closures sent to the worker functions. The shared data are received from the `$lock` closure and sent to the `$unlock` closure. The last value sent can be get invoking the `WorkerPool::lastValue()`

```
use hdvianna\Concurrent\WorkFactoryInterface;
use hdvianna\Concurrent\WorkerPool;

$sharedData = 700;
$works = 1000;

$pool = new WorkerPool((new class ($sharedData, $works) implements WorkFactoryInterface {

    /**
     * @var int
     */
    private $sharedData;

    /**
     * @var int
     */
    private $works;

    /***
     *  constructor.
     * @param int $sharedData
     * @param int $works
     */
    public function __construct($sharedData, $works)
    {
        $this->works = $works;
        $this->sharedData = $sharedData;
    }

    public function createWorkGeneratorClosure(): \Closure
    {
        $workers = $this->works;
        return function () use ($workers) {
            for ($i = 0; $i < $workers; $i++) {
                $work = new \stdClass();
                $work->value = 1;
                yield $work;
            }
        };
    }

    public function createWorkConsumerClosure(): \Closure
    {
        $initialValue = $this->sharedData;
        //Use the $lock and $unlock closures to synchronize data
        return function ($work, $lock, $unlock) use ($initialValue) {
            /*Synchronize the data. Will block and wait for data.
            $lock will return the last value*/
            $shared = $lock();
            if (!isset($shared)) {
                //Data was not initialized
                $shared = $initialValue;
            }
            $shared += $work->value;
            //Unlocks sending the new data.
            $unlock($shared);
        };
    }

}), 10);
$pool->run();
//Get the last value sent to the unlock closure
$result = $pool->lastValue();
echo("\$result equals to \$works + \$sharedData?" . PHP_EOL);
echo("($result equals to $works + $sharedData?)" . PHP_EOL);
echo(assert($result === ($works + $sharedData)) ? "Yes!": "No =(").PHP_EOL;
```

###  Health Score

26

—

LowBetter than 41% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity10

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity57

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~45 days

Recently: every ~56 days

Total

6

Last Release

2130d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/d766f4c3a7bd6f013a4885d201a412c314bb0f8f8c4589fe17b9ad9a4df30574?d=identicon)[hdvianna](/maintainers/hdvianna)

---

Top Contributors

[![hdvianna](https://avatars.githubusercontent.com/u/1439957?v=4)](https://github.com/hdvianna "hdvianna (45 commits)")

---

Tags

parallelparallel-computingphpworkerworker-pool

### Embed Badge

![Health badge](/badges/hdvianna-parallel-workerpool/health.svg)

```
[![Health](https://phpackages.com/badges/hdvianna-parallel-workerpool/health.svg)](https://phpackages.com/packages/hdvianna-parallel-workerpool)
```

###  Alternatives

[league/geotools

Geo-related tools PHP 7.3+ library

1.4k5.6M31](/packages/league-geotools)[illuminate/bus

The Illuminate Bus package.

6046.3M541](/packages/illuminate-bus)[uecode/qpush-bundle

Asynchronous processing for Symfony using Push Queues

1672.5M2](/packages/uecode-qpush-bundle)[belvg/module-sqs

N/A

1544.6k](/packages/belvg-module-sqs)[bsidev/bitrix-queue

Queues for Bitrix CMS

232.8k](/packages/bsidev-bitrix-queue)[lelivrescolaire/sqs-bundle

Communicate with an SQS queue from inside your symfony 2 application.

109.6k](/packages/lelivrescolaire-sqs-bundle)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
