PHPackages                             kynx/swoole-processor - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. kynx/swoole-processor

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

kynx/swoole-processor
=====================

Execute tasks in multi-process environment

0.4.0(1y ago)037MITPHPPHP ~8.3.0 || ~8.4.0CI passing

Since Jan 20Pushed 1y ago1 watchersCompare

[ Source](https://github.com/kynx/swoole-processor)[ Packagist](https://packagist.org/packages/kynx/swoole-processor)[ RSS](/packages/kynx-swoole-processor/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (7)Dependencies (6)Versions (9)Used By (0)

kynx/swoole-processor
=====================

[](#kynxswoole-processor)

[![Continuous Integration](https://github.com/kynx/swoole-processor/actions/workflows/continuous-integration.yml/badge.svg)](https://github.com/kynx/swoole-processor/actions/workflows/continuous-integration.yml)

Run CLI batch jobs in coroutines across multiple processes.

Based on [Swoole](https://wiki.swoole.com), the processor is ideal for running a large number of IO-intensive operations.

Install
-------

[](#install)

```
composer require kynx/swoole-processor

```

Use
---

[](#use)

Create a `JobProvider` class that returns `Job` objects containing the payload you want to process:

```
use Kynx\Swoole\Processor\Job\Job;
use Kynx\Swoole\Processor\Job\JobProviderInterface;

class JobProvider implements JobProviderInterface
{
    public function getIterator(): Generator
    {
        foreach (range(0, 10) as $payload) {
            // NOTE: your payload must be serializable!!
            yield new Job($payload);
        }
    }
}
```

Create a `Worker` class to handle the jobs:

```
use Kynx\Swoole\Processor\Job\WorkerInterface;

class Worker implement WorkerInterface
{
    public function init(int $workerId): void
    {
        // perform any initialisation needed
    }

    public function run(Job $job): Job
    {
        // do work on payload
        echo "Got payload: " . $job->getPayload() . "\n";

        // return job with result of process
        return $job->withResult("Processed: " . $job->getPayload());
    }
}
```

If required, create a `CompletionHandler` to do any post-processing:

```
use Kynx\Swoole\Processor\Job\CompletionHandlerInterface;

class CompletionHandler implements CompletionHandlerInterface
{
    public function complete(Job $job): void
    {
        // mark job as complete
        echo "Completed: " . $job->getResult() . "\n";
    }
}
```

Create a script to run the jobs in parallel:

```
use Kynx\Swoole\Processor\Config;
use Kynx\Swoole\Processor\Processor;
use Throwable;

$processor = new Processor(
    new Config(),
    new JobProvider(),
    new Worker(),
    new CompletionHandler()
);

// this will block until all jobs are processed
$result = $processor->run();
return $result === true ? 0 : 1;
```

If you don't need to handle completion, omit the fourth parameter.

How it works
------------

[](#how-it-works)

The `JobProvider` runs in its own process, and the resulting jobs are fired at a Swoole server listening on a local socket. Both it and the the `CompletionHandler` are **blocking**. If the `JobProvider` returns a large number of jobs or performs time-consuming operations, return a Generator so jobs can be started as soon as possible. The `CompletionHandler` should be fast.

The server spawns a number of processes to handle the jobs - by default one less than the number of CPU cores detected. The process runs your `Worker` inside a [coroutine](https://wiki.swoole.com/en/#/start/coroutine), ensuring IO operations do not block. Because of this, ensure all IO uses a [Connection Pool](https://wiki.swoole.com/en/#/coroutine/conn_pool), covered in more detail below.

You can control the number of simultaneous jobs the processor will spawn by setting the `concurrency` parameter on the the `Config` you pass to the constructor. It defaults to 10, with a maximum of `workers` x `maxCoroutines`.

Caveats
-------

[](#caveats)

- `Job` payloads and results **must be serializable** and, but default, together should be less than 2M when serialized
- A `Worker` should be **stateless**. If you need share data between workers, use a [Table](https://wiki.swoole.com/en/#/memory/table).
- Uncaught exceptions will crash the process, causing it it re-spawn. For this reason your `Worker::run()` is called inside a `try ... catch` block. However the exception is discarded: if you care about where your program is going wrong, catch all exceptions inside your `Worker` and handle them yourself.

###  Health Score

29

—

LowBetter than 59% of packages

Maintenance42

Moderate activity, may be stable

Popularity7

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity51

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 ~1 days

Total

7

Last Release

476d ago

### Community

Maintainers

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

---

Top Contributors

[![kynx](https://avatars.githubusercontent.com/u/1320145?v=4)](https://github.com/kynx "kynx (10 commits)")

###  Code Quality

TestsPHPUnit

Static AnalysisPsalm

Code StylePHP\_CodeSniffer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/kynx-swoole-processor/health.svg)

```
[![Health](https://phpackages.com/badges/kynx-swoole-processor/health.svg)](https://phpackages.com/packages/kynx-swoole-processor)
```

PHPackages © 2026

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