PHPackages                             misterion/ko-process - 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. [CLI &amp; Console](/categories/cli)
4. /
5. misterion/ko-process

ActiveLibrary[CLI &amp; Console](/categories/cli)

misterion/ko-process
====================

Simple pcntl fork wrapper and process manager

v0.5.3(10y ago)177337.7k↓33.1%23[3 issues](https://github.com/misterion/ko-process/issues)[1 PRs](https://github.com/misterion/ko-process/pulls)6MITPHPPHP &gt;=5.4.0

Since Feb 14Pushed 8y ago10 watchersCompare

[ Source](https://github.com/misterion/ko-process)[ Packagist](https://packagist.org/packages/misterion/ko-process)[ Docs](https://github.com/misterion/ko-process)[ RSS](/packages/misterion-ko-process/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)Dependencies (2)Versions (11)Used By (6)

ko-process
==========

[](#ko-process)

[![Build Status](https://camo.githubusercontent.com/8266aef7fa4be5bffc6831af91073910f3eb19dc95e1b1e0a9ea0454f88ef3ed/68747470733a2f2f7472617669732d63692e6f72672f6d6973746572696f6e2f6b6f2d70726f636573732e706e673f6272616e63683d6d6173746572)](https://travis-ci.org/misterion/ko-process)[![Latest Stable Version](https://camo.githubusercontent.com/9af3efd5ec0e5f0d0752c806d325953333f4a3953f87850873bfc22ac08a79e8/68747470733a2f2f706f7365722e707567782e6f72672f6d6973746572696f6e2f6b6f2d70726f636573732f762f737461626c652e706e67)](https://packagist.org/packages/misterion/ko-process)[![Code Coverage](https://camo.githubusercontent.com/1c4442721c3302f6cf95edc3d9f620d2baf6445a5e50ac7f63dbde52fe393b7b/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6d6973746572696f6e2f6b6f2d70726f636573732f6261646765732f636f7665726167652e706e673f733d35626265353036356432333066633639653131653363333437343766656337323464336464366436)](https://scrutinizer-ci.com/g/misterion/ko-process/)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/b3132f2c54480acee025b9ff38d6a790c380976f46f07a3b50b4ae1d4fd0ce3e/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6d6973746572696f6e2f6b6f2d70726f636573732f6261646765732f7175616c6974792d73636f72652e706e673f733d35383734376365653335363934633462633262303233623265653561363666343036366465336637)](https://scrutinizer-ci.com/g/misterion/ko-process/)[![Total Downloads](https://camo.githubusercontent.com/424453ca5e2a9f88da1af54d4218d034de145f33df87c47b819f919c60160c24/68747470733a2f2f706f7365722e707567782e6f72672f6d6973746572696f6e2f6b6f2d70726f636573732f646f776e6c6f6164732e706e67)](https://packagist.org/packages/misterion/ko-process)[![Latest Unstable Version](https://camo.githubusercontent.com/53ec2054d74fdd347af26abb2688b35a1149b41a1ce62b9299154976cfd33a35/68747470733a2f2f706f7365722e707567782e6f72672f6d6973746572696f6e2f6b6f2d70726f636573732f762f756e737461626c652e706e67)](https://packagist.org/packages/misterion/ko-process)[![License](https://camo.githubusercontent.com/e838facf53c045e20c4df97df19737881767ce5700063e076d05173253a2ceb7/68747470733a2f2f706f7365722e707567782e6f72672f6d6973746572696f6e2f6b6f2d70726f636573732f6c6963656e73652e706e67)](https://packagist.org/packages/misterion/ko-process)

Ko-Process allows for easy callable forking. It is object-oriented wrapper around fork part of [`PCNTL`](http://php.net/manual/ru/book.pcntl.php) PHP's extension. Background process, detaching process from the controlling terminal, signals and exit codes and simple IPC via shared memory. This is well tested library used in real world high load projects.

Installation
============

[](#installation)

### Requirements

[](#requirements)

```
PHP >= 5.4
pcntl extension installed
posix extension installed

```

### Via Composer

[](#via-composer)

The recommended way to install library is [composer](http://getcomposer.org). You can see [package information on Packagist](https://packagist.org/packages/misterion/ko-process).

```
{
	"require": {
		"misterion/ko-process": "*"
	}
}
```

### Do not use composer?

[](#do-not-use-composer)

Just clone the repository and care about autoload for namespace `Ko`.

Usage
=====

[](#usage)

Basic usage looks like this:

```
$manager = new Ko\ProcessManager();
$process = $manager->fork(function(Ko\Process $p) {
    echo 'Hello from ' . $p->getPid();
})->onSuccess(function() {
    echo 'Success finish!';
})->wait();
```

If should wait for all forked process

```
$manager = new Ko\ProcessManager();
for ($i = 0; $i < 10; $i++) {
    $manager->fork(function(Ko\Process $p) {
        echo 'Hello from ' . $p->getPid();
        sleep(1);
    });
}
$manager->wait();
```

### Process title?

[](#process-title)

Yes, both `ProcessManager` and `Process` can change process title with `setProcessTitle` function. Or you may use trait Ko\\Mixin\\ProcessTitle to add this to any class you want. Take attention about `ProcessManager::onShutdown` - use can set callable which would be called if `ProcessManager` catch `SIGTERM`. The handler would be called before child process would be shutdown. We use `demonize` to detach from terminal. Run sample with code

```
$manager = new Ko\ProcessManager();
$manager->demonize();
$manager->setProcessTitle('I_am_a_master!');
$manager->onShutdown(function() use ($manager) {
    echo 'Catch sigterm.Quiting...' . PHP_EOL;
    exit();
});

echo 'Execute `kill ' . getmypid() . '` from console to stop script' . PHP_EOL;
while(true) {
    $manager->dispatch();
    sleep(1);
}
```

and `ps aux|grep I_am_a_master` or `top` to see you process title in linux process list.

### Spawn

[](#spawn)

Making master - child process pattern application you should care about child process be alive. The `spawn` function will help you with that - once `spawn` will keep forked process alive after he exit with some error code.

```
$manager = new Ko\ProcessManager();
for ($i = 0; $i < 10; $i++) {
    $manager->spawn(function(Ko\Process $p) {
        echo 'Hello from ' . $p->getPid();
        sleep(1);
        exit(1); //exit with non 0 exit code
    });
}
$manager->wait(); //we have auto respawn for 10 forks
```

Let`s explain you are writing something like queue worker based on PhpAmqpLib\\AMPQ. So you can write something like this

```
use PhpAmqpLib\Connection\AMQPConnection;
use PhpAmqpLib\Message\AMQPMessage;

$manager = new Ko\ProcessManager();
$manager->setProcessTitle('Master:working...');
$manager->spawn(function(Ko\Process $p) {
    $connection = new AMQPConnection('localhost', 5672, 'guest', 'guest');
    $channel = $connection->channel();

    $channel->queue_declare('hello', false, true, false, false);

    $callback = function($msg) use (&$p) {
        $p->setProcessTitle('Worker:processJob ' . $msg->body);

        //will execute our job in separate process
        $m = new Ko\ProcessManager();
        $m->fork(function(Ko\Process $jobProcess) use ($msg) {
            $jobProcess->setProcessTitle('Job:processing ' . $msg->body);

            echo " [x] Received ", $msg->body, "\n";
            sleep(2);
            echo " [x] Done", "\n";
        })->onSuccess(function() use ($msg){
            //Ack on success
            $msg->delivery_info['channel']
                ->basic_ack($msg->delivery_info['delivery_tag']);
        })->wait();

        $p->setProcessTitle('Worker:waiting for job... ');

        //IMPORTANT! You should call dispatch() them self to process pending signals.
        $p->dispatch();

        if ($p->isShouldShutdown()) {
            exit();
        }
    };

    $channel->basic_qos(null, 1, null);
    $channel->basic_consume('hello', '', false, false, false, false, $callback);

    while(count($channel->callbacks)) {
        $channel->wait();
    }

    $channel->close();
    $connection->close();
});
$manager->wait();
```

### Shared memory and Semaphore

[](#shared-memory-and-semaphore)

The `Ko\SharedMemory` used `Semaphore` for internal locks so can be safely used for inter process communications. SharedMemory implements `\ArrayAccess` and `\Countable` interface so accessible like an array:

```
$sm = new SharedMemory(5000); //allocate 5000 bytes
$sm['key1'] = 'value';

echo 'Total keys is' . count($sm) . PHP_EOL;
echo 'The key with name `key1` exists: ' . isset($sm['key1'] . PHP_EOL;
echo 'The value of key1 is ' . $sm['key1'] . PHP_EOL;

unset($sm['key1']);
echo 'The key with name `key1` after unset exists: ' . isset($sm['key1'] . PHP_EOL;
```

You can use `Semaphore` for inter process locking:

```
$s = new Semaphore();
$s->acquire();
//do some job
$s->release();

//or
$s->tryExecute(function() {
    //do some job
});
```

### Credits

[](#credits)

Ko-process written as a part of [GameNet project](http://gamenet.ru) by Nikolay Bondarenko (misterionkell at gmail.com).

### License

[](#license)

Released under the [MIT](LICENSE) license.

### Links

[](#links)

- [Project profile on the Ohloh](https://www.ohloh.net/p/ko-process)

###  Health Score

41

—

FairBetter than 89% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity51

Moderate usage in the ecosystem

Community27

Small or concentrated contributor base

Maturity54

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 90.8% 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 ~86 days

Total

10

Last Release

3696d ago

### Community

Maintainers

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

---

Top Contributors

[![misterion](https://avatars.githubusercontent.com/u/196737?v=4)](https://github.com/misterion "misterion (79 commits)")[![damoclark](https://avatars.githubusercontent.com/u/275110?v=4)](https://github.com/damoclark "damoclark (3 commits)")[![duncan3dc](https://avatars.githubusercontent.com/u/546811?v=4)](https://github.com/duncan3dc "duncan3dc (3 commits)")[![milo-container](https://avatars.githubusercontent.com/u/2306671?v=4)](https://github.com/milo-container "milo-container (1 commits)")[![ovr](https://avatars.githubusercontent.com/u/572096?v=4)](https://github.com/ovr "ovr (1 commits)")

---

Tags

cliutilitysignalposixsemaphoreAMQPbackgroundforkThreadpcntlshared memoryexit\_code

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/misterion-ko-process/health.svg)

```
[![Health](https://phpackages.com/badges/misterion-ko-process/health.svg)](https://phpackages.com/packages/misterion-ko-process)
```

###  Alternatives

[misterion/ko-worker

Ko-worker project is a base to develop amqp based message dispatchers

2713.4k](/packages/misterion-ko-worker)[zhgzhg/gphpthread

Generic PHP Threads library using only pure PHP

154.1k](/packages/zhgzhg-gphpthread)[phlib/console-process

Console implementation.

1833.5k2](/packages/phlib-console-process)[j13k/yaml-lint

A compact command line utility for checking YAML file syntax

161.1M19](/packages/j13k-yaml-lint)[arara/process

Provides a better API to work with processes on Unix-like systems

16861.7k2](/packages/arara-process)[amercier/cli-helpers

Utility classes to write PHP command-line scripts

258.8k2](/packages/amercier-cli-helpers)

PHPackages © 2026

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