PHPackages                             besanek/threads - 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. besanek/threads

ActiveLibrary

besanek/threads
===============

Fake threads in PHP

3133PHP

Since May 13Pushed 12y ago1 watchersCompare

[ Source](https://github.com/besanek/threads)[ Packagist](https://packagist.org/packages/besanek/threads)[ RSS](/packages/besanek-threads/feed)WikiDiscussions master Synced 4d ago

READMEChangelogDependenciesVersions (2)Used By (0)

Fake threads in PHP
===================

[](#fake-threads-in-php)

\##Why?

- simple usage
- no need any PHP extension

Install
-------

[](#install)

The best way to install is using Composer:

```
$ composer require besanek/threads:@dev
```

\##Usage

### Basic

[](#basic)

It's simple!

```
use Besanek\Threads;

// ...
$executable = new Threads\Executables\PhpExecutable('path/to/script.php');
$job = new Threads\Job($executable);
$job->run();
```

You can write your own Executable that provides you running bash scripts, native apllications, inline PHP code and whatever you want. Just implement the `Besanek\Threads\IExecutable` interface.

### Let's work with the job

[](#lets-work-with-the-job)

Sometimes we read output and sometimes we need write some imput

```
$executable = new Threads\Executables\PhpExecutable('path/to/repeat.php');
$job = new Threads\Job($executable);
$job->run();
$job->write('Hello');
echo $job->read(); //Hello;
```

> WARNING! The subscript runs in other process, so there can be latency at writing and reading. In this case is not guaranteed that `read()` return 'Hello'. That occurs if subscript is slower than current script. The solution is waiting cycle like this.
>
> ```
> while(empty($output = $job->read())) {}
> ```

```

### It's OK or NOT?

How to react to end of the subscript? Callbacks!

```php
$ok = function () { echo "OK" };
$fail = function () { echo "FAIL" };
$job = new Threads\Job($executable, $ok, $fail);

```

To both callback are passed 3 parameters. stdout, stderr and exitcode.

```
$ok = function ($stdout) { echo $stdout };
$fail = function ($stdout, $stderr, $exitcode) {
    echo "FAILS with exitcode: " . $exitcode . " and error: " . $stderr;
};
```

> NOTE: Callbacks are processed in the method `isDone()` witch is by default called on an object destructor. It can be too late. If you need process the callback sooner. Run the method `isDone()` manualy. Ideally in a waiting cycle in case of a subscript is still running.
>
> ```
> while($job->isDone() === false) {}
> ```

```

### At the end is here management

Imagine, you need resize thousand of pictures. It's ideal for threads! But, if I creates thousand of subprocesses, I wastes all sources on my machine. Is there solution?

**Yes!** `Besanek\Threads\Runner`.

You can add set of jobs and limit the number of threads.

```php
$images = // do some black magic

$runner = new Threads\Runner(50); //Max 50 subprocess
foreach($images as $image) {
    $arguments = '-size=1000x1000 -output output/path/' . $image->name . '--source '.$image->path;
    $executable = new Threads\Executables\PhpExecutable('path/to/resize.php', $arguments);
    $job = new Threads\Job($executable);
    $runner->addJob($job);
    $runner->process();
}

```

> INFO: Method `process()` has one argument, wich determines whether a queue of jobs are done completely. So if you calling `process(true)` the whole queue is gradually runned, but it block main process while all jobs aren't done. This is by default called in destructor of runner.

> WARNING! Only in `process()` method are jobs runned and finished. So if you forget call it, jobs are waiting till end of script.

###  Health Score

23

—

LowBetter than 27% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity13

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity43

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.

### Community

Maintainers

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

---

Top Contributors

[![besanek](https://avatars.githubusercontent.com/u/3389310?v=4)](https://github.com/besanek "besanek (9 commits)")

### Embed Badge

![Health badge](/badges/besanek-threads/health.svg)

```
[![Health](https://phpackages.com/badges/besanek-threads/health.svg)](https://phpackages.com/packages/besanek-threads)
```

PHPackages © 2026

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