PHPackages                             soarce/parallel-process-dispatcher - 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. soarce/parallel-process-dispatcher

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

soarce/parallel-process-dispatcher
==================================

Tiny PHP library for running jobs in background and/or in parallel

6.2.1(1y ago)45.2k↓78%1[1 issues](https://github.com/soarce-qa/parallel-process-dispatcher/issues)1MITPHPPHP &gt;=8.0

Since Aug 31Pushed 1y agoCompare

[ Source](https://github.com/soarce-qa/parallel-process-dispatcher)[ Packagist](https://packagist.org/packages/soarce/parallel-process-dispatcher)[ RSS](/packages/soarce-parallel-process-dispatcher/feed)WikiDiscussions master Synced yesterday

READMEChangelog (3)Dependencies (8)Versions (26)Used By (1)

soarce/parallel-process-dispatcher [![Build Status](https://camo.githubusercontent.com/4d736cf9fa55d131e2d4992ec7b4bafc96871abeb44b3104efcc1a69ccf117ba/68747470733a2f2f7472617669732d63692e636f6d2f736f617263652f706172616c6c656c2d70726f636573732d646973706174636865722e7376673f6272616e63683d6d6173746572)](https://travis-ci.com/soarce/parallel-process-dispatcher) [![Packagist](https://camo.githubusercontent.com/a1a9740b615b39759dca71d073dd19ff3de44899928d9365e8dfa7629f39cd24/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f736f617263652f706172616c6c656c2d70726f636573732d646973706174636865722e737667)](https://packagist.org/packages/soarce/parallel-process-dispatcher)
====================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================

[](#soarceparallel-process-dispatcher--)

This micro-library has two classes. One encapsulates a (linux commandline) process into an object and allows asynchronous running without deadlocks. The other is a multi-process-dispatcher which takes an arbitrary number of beforementioned processes and runs them simultaneously (with a maximum number of concurrent processes).

### Usage examples:

[](#usage-examples)

- Dispatching long running cronjobs which e.g. mostly wait for webservice responses (you can run more processes than max. CPUs)
- Running background workers which listen on a queue (maximum should be number of CPUs)
- Running commandline-tasks inside a web application simultaneously, e.g. PDF-Generation, Image-Processing etc.

### Installation:

[](#installation)

Add the following to your `composer.json`:

```
{
    "require": {
        "soarce/parallel-process-dispatcher": "*"
    }
}
```

or just run the following command in your project root directory

```
$ composer require "soarce/parallel-process-dispatcher"
```

Usage
-----

[](#usage)

### Classes

[](#classes)

#### Process

[](#process)

```
$process = new Process('pngcrush --brute background.png');
$process->start();

// optional: do something else in your application

while (! $process->isFinished() ) {
    usleep(1000); //wait 1ms until next poll
}
echo $process->getOutput();
```

#### Dispatcher

[](#dispatcher)

```
$process1 = new Process('pngcrush --brute background.png');
$process2 = new Process('pngcrush --brute welcome.png');
$process3 = new Process('pngcrush --brute logo.png');

$dispatcher = new Dispatcher(2);    // will make sure only two of those will actually run at the same time
$dispatcher->addProcess($process1);
$dispatcher->addProcess($process2);
$dispatcher->addProcess($process3);

$dispatcher->dispatch();  // this will run until all processes are finished.

$processes = $dispatcher->getFinishedProcesses();

foreach ($processes as $process) {
    echo $process->getOutput(), "\n\n";
}
```

### Advanced

[](#advanced)

#### Using Process and Dispatcher to start multiple processes and later collect the results

[](#using-process-and-dispatcher-to-start-multiple-processes-and-later-collect-the-results)

```
$dispatcher = new Dispatcher(2);

$process1 = new Process('pngcrush --brute background.png');
$dispatcher->addProcess($process1, true);   // true starts the process if there are still free slots

// [... more code ...]

$process2 = new Process('pngcrush --brute welcome.png');
$dispatcher->addProcess($process2, true);

// [... more code ...]

// during code execution, the dispatcher cannot remove finished processes from the stack, so you have to call the tick()-function
// if you want the queue to advance - but it's optional since at latest the __destruct() function will call dispatch();
$dispatcher->tick();

// [... more code ...]

$dispatcher->dispatch();  // this will make the dispatcher wait until all the processes are finished, if they are still running

$processes = $dispatcher->getFinishedProcesses();

// loop over results
```

#### Reading Output from multiple jobs while they are running

[](#reading-output-from-multiple-jobs-while-they-are-running)

A possible use case for this would be running multiple crawlers over separate slow(er) filesystems or websites to generate a list of certain download or backup worthy files while the main process keeps track of the list, eliminates duplicates and writes the backup.

```
$dispatcher = new Dispatcher(2);

$dispatcher->addProcess(new ProcessLineOutput("...", 'job1'));
$dispatcher->addProcess(new ProcessLineOutput("...", 'job2'));
$dispatcher->addProcess(new ProcessLineOutput("...", 'job3'));

$oa = new OutputAggregator($dispatcher);
foreach ($oa->getOutput() as $job => $line) {
    echo $job, ': ', $line;
}
```

The function `OutputAggregator::getOutput()` returns a Generator which returns the job's name (here job1-3) as key and the output line as value. Contrary to arrays, the key will with almost certain probability appear multiple times.

Known Issues
------------

[](#known-issues)

### Process

[](#process-1)

- PHP Internals: Be aware that if the child process produces output, it will write into a buffer until the buffer is full. If the buffer is full the child pauses until the parent reads from the buffer and makes more room. This is done in the isFinished() method. The dispatcher calls this method periodically to prevent a deadlock. If you use the process class standalone, you have multiple possibilities to prevent this:
    - call isFinished() yourself in either a loop, using a tick function or otherwise during execution of your script
    - instead of writing to stdOut, divert output to a temporary file and use its name as output.
    - use the combination of OutputAggregator and ProcessLineOutput and work on output as soon as it arrives. This might consume a lot of RAM for buffers if your jobs generate a lot of output, so set high enough limits.

### Dispatcher

[](#dispatcher-1)

- Multiple dispatchers (in different processes) are not aware of each other. So if you have a script that uses a dispatcher to call another script which itself uses a dispatcher to spawn multiple processes, you will end up with more child processes than the maximum, so choose the maximum accordingly or use a queue (e.g. Redis) and make the workers aware of each other by e.g. registering in a redis-stack for running workers.

###  Health Score

44

—

FairBetter than 90% of packages

Maintenance44

Moderate activity, may be stable

Popularity26

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity78

Established project with proven stability

 Bus Factor1

Top contributor holds 79.5% 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 ~169 days

Recently: every ~333 days

Total

20

Last Release

378d ago

Major Versions

2.0.0 → 3.0.02020-02-29

3.0.0 → 4.0.02020-02-29

2.1.0 → 4.1.12020-03-21

2.1.1 → 5.0.02021-10-24

5.0.0 → 6.0.02022-02-05

PHP version history (9 changes)1.0.0PHP &gt;=5.4,&lt;8.0-DEV

1.2.2PHP &gt;=5.3

3.0.0PHP &gt;=7.0,&lt;8.0-DEV

4.0.0PHP &gt;=7.1,&lt;8.0-DEV

2.1.0PHP &gt;=5.5,&lt;8.0-DEV

5.0.0PHP &gt;=7.4,&lt;8.1-DEV

6.0.0PHP &gt;=8.0,&lt;8.2-DEV

6.1.0PHP &gt;=8.0,&lt;8.3

6.2.0PHP &gt;=8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/295944cfad74862d4d4038cdea8c6bb68e2c4d5e2339042e9050d6b2df6d27b3?d=identicon)[hsegnitz](/maintainers/hsegnitz)

---

Top Contributors

[![hsegnitz](https://avatars.githubusercontent.com/u/2615958?v=4)](https://github.com/hsegnitz "hsegnitz (31 commits)")[![bahlo](https://avatars.githubusercontent.com/u/1725839?v=4)](https://github.com/bahlo "bahlo (7 commits)")[![wowpatrick](https://avatars.githubusercontent.com/u/349524?v=4)](https://github.com/wowpatrick "wowpatrick (1 commits)")

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/soarce-parallel-process-dispatcher/health.svg)

```
[![Health](https://phpackages.com/badges/soarce-parallel-process-dispatcher/health.svg)](https://phpackages.com/packages/soarce-parallel-process-dispatcher)
```

###  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)[mayconbordin/l5-stomp-queue

Stomp Queue Driver for Laravel 5

121.1k](/packages/mayconbordin-l5-stomp-queue)

PHPackages © 2026

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