PHPackages                             pulyavin/thread-pool - 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. pulyavin/thread-pool

ActiveLibrary

pulyavin/thread-pool
====================

Imitation of threads in PHP

7512[1 issues](https://github.com/pulyavin/thread-pool/issues)PHP

Since Oct 31Pushed 8y ago2 watchersCompare

[ Source](https://github.com/pulyavin/thread-pool)[ Packagist](https://packagist.org/packages/pulyavin/thread-pool)[ RSS](/packages/pulyavin-thread-pool/feed)WikiDiscussions master Synced today

READMEChangelogDependenciesVersions (1)Used By (0)

PHP Thread Pool
===============

[](#php-thread-pool)

This library help to simply write PHP-code working in parallel processes

Installation
------------

[](#installation)

1. Install [Composer](http://getcomposer.org/):

    ```
    curl -sS https://getcomposer.org/installer | php

    ```
2. Add the dependency:

    ```
    php composer.phar require pulyavin/thread-pool:dev-master

    ```

Usage
-----

[](#usage)

### Runnable

[](#runnable)

```
use League\ThreadPool\Interfaces\RunnableInterface;

class WorkerRunnable extends RunnableInterface
{
    use \League\ThreadPool\RunnableTrait;

    /**
     * @var LoggerInterface
     */
    private $logger;

    public function __construct(LoggerInterface $logger)
    {
        $this->logger = $logger;
    }

    /**
     * {@inheritdoc}
     */
    public function run()
    {
        try {
            while (true) {
                $this->logger->debug('We are working');
                $this->dispatchSignals();

                sleep(1);
            }
        }
        catch (InterruptedException $e) {
            $this->logger->debug('shutdown');

            return 0;
        }
        catch (\Exception $e) {
            return 1;
        }

        return 0;
    }
}
```

### Thread

[](#thread)

```
$runnable = new WorkerRunnable($logger);

$thread = new \League\ThreadPool\Thread($runnable);
$thread->setName("my awesome process");
$thread->registerShutdownFunction(function () {
    unset("/var/run/process.pid");
});

$thread->start();
$thread->wait();
$thread->read();

$exitCode = $thread->readExitCode();
```

### ThreadPool

[](#threadpool)

```
$threadPool = new \League\ThreadPool\ThreadPool;

for ($i = 0; $i < 5; $i++) {
    $runnable = new WorkerRunnable($logger);

    $thread = new \League\ThreadPool\Thread($runnable);
    $thread->setName("my awesome process #{$i}");

    $threadPool->submit($thread);
}

$threadPool->join(true);
```

### Daemon

[](#daemon)

```
$runnable = new WorkerRunnable;
$thread = new \League\ThreadPool\Thread($runnable);
$thread->setName("my awesome process");

$daemon = new League\ThreadPool\Daemon($thread, "/path/to/pid/file");
$daemon->run();
```

### Daemon as service

[](#daemon-as-service)

```
#!/usr/bin/env php
