PHPackages                             reptily/async-run - 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. reptily/async-run

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

reptily/async-run
=================

Library for asynchronous launch

3.1(2mo ago)16GPL-3.0-or-laterPHPPHP &gt;=8

Since May 5Pushed 2mo ago1 watchersCompare

[ Source](https://github.com/reptily/async-run)[ Packagist](https://packagist.org/packages/reptily/async-run)[ RSS](/packages/reptily-async-run/feed)WikiDiscussions master Synced 1w ago

READMEChangelog (4)DependenciesVersions (5)Used By (0)

Install
=======

[](#install)

```
composer require reptily/async-run
```

### Example use as object

[](#example-use-as-object)

Example file: [/example/async\_run.php](https://github.com/reptily/async-run/blob/master/example/async_run.php)

This library provides several methods for working with asynchronicity.

When an object is initialized in the constructor, parameters are available.

```
new AsyncRun(
    int $coresCount = 1,             // Count of running threads
    string $pathTmpDir = '/tmp',     // path to temporary files directory
    ?string $lockFileName = null     // filename for locale
);
```

### Example use as callbacks

[](#example-use-as-callbacks)

Example file: [/example/async.php](https://github.com/reptily/async-run/blob/master/example/async.php)

You can easily use the library within your code.

To do this, fill in the necessary functions in the **run(...function)** method

After this, all the functions specified in run() will be executed, if there is success, **then()** will be called, if there is an error, **catch()** will be called, the **finally()** method will be called in any of the above cases.

```
Async::run(
    function () {
        sleep(1);
        echo "AAA" . PHP_EOL;
    },
    function () {
        echo "BBB" . PHP_EOL;
    }
)->then(function () {
    echo "CCC" . PHP_EOL;
})->finally(function () {
    echo "DDD" . PHP_EOL;
})->catch(function ($errorText) {
    echo "Error " . $errorText . PHP_EOL;
});
```

### Example with Shared Memory and Mutex

[](#example-with-shared-memory-and-mutex)

Example file: [/example/async\_memory.php](https://github.com/reptily/async-run/blob/master/example/async_memory.php)

This example demonstrates how to use shared memory and mutex for thread-safe operations in asynchronous execution.

**Performance Note:** In SharedMemoryContainer, both numeric and string keys can be used with `get()` and `set()` methods, but using numeric keys provides better performance.

```
$fns = [];
for ($i = 1; $i lock();
        $data = $memory->get('transaction') ?? ['moneySeller' => 0 , 'moneyBuyer' => 1000];
        $data['moneySeller']++;
        $data['moneyBuyer']--;
        $memory->set('transaction', $data);
        $mutex->unlock();
    };
}

Async::run(...$fns)->finally(function (array $memory) {
    var_dump($memory);
    /*
    array(1) {
        'transaction' =>
            array(2) {
                'moneySeller' => int(500)
                'moneyBuyer' => int(500)
            }
    }
    */
});
```

### Example: Race Conditions and Solutions

[](#example-race-conditions-and-solutions)

Example file: [/example/async\_race\_conditions.php](https://github.com/reptily/async-run/blob/master/example/async_race_conditions.php)

This example demonstrates race conditions in concurrent programming and shows how to solve them using mutex locks.

**Problem 1: Variable scope issues**

```
$fns = [];
$money = 0;
for ($i = 1; $i  error (variable scope issue)
```

**Problem 2: Race conditions with shared memory**

```
$fns = [];
for ($i = 1; $i get(1) ?? 0;
        $money++;
        $memory->set(1, $money);
    };
}
Async::run(...$fns)->finally(function (array $memory) {
    echo $memory[1] . PHP_EOL; // 998 != 1000 (race condition)
});
```

**Solution: Using Mutex for thread-safe operations**

```
$fns = [];
for ($i = 1; $i lock();
        $money = $memory->get(1) ?? 0;
        $money++;
        $memory->set(1, $money);
        $mutex->unlock();
    };
}
Async::run(...$fns)->finally(function (array $memory) {
    echo $memory[1] . PHP_EOL; // 1000 (correct result)
});
```

### Methods

[](#methods)

*init* - Object initialization method, used for prelaunch configuration.

```
protected function init(): void
{
    $this->arrayTest = [
        [self::FIELD_NAME => 'Bob'],
        [self::FIELD_NAME => 'Mark'],
        [self::FIELD_NAME => 'Ana'],
    ];
}
```

*getError* - Error return method.

```
protected function getError(string $message): void
{
    print_r($message);
}
```

*done* - The method is run after the entire execution, as a rule, it serves to generate a report.

```
protected function done(): void
{
    echo "Done in " . $this->getProgressTime() . " sec.\n";
}
```

*unlock* - Method for pre-unblocking.

```
(new Example(2))->unlock();
```

*run* - Method to run handlers.

```
(new Example(2))->run();
```

*workerAfterSpawn* - Method starts before each worker and distributes tasks to them.

```
protected function workerAfterSpawn(): void
{
    next($this->arrayTest);
}
```

*getWorkerResults* - The method returns the result of the worker's work.

```
protected function getWorkerResults(): void
{
    $item = current($this->arrayTest);
    echo $item[self::FIELD_NAME] . "\n";
}
```

*getWorkerDoneCondition* - The method serves as a pointer to the completion of processing all jobs.

```
protected function getWorkerDoneCondition(): bool
{
    return current($this->arrayTest) === false;
}
```

###  Health Score

39

—

LowBetter than 86% of packages

Maintenance85

Actively maintained with recent releases

Popularity6

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

Total

4

Last Release

80d ago

Major Versions

1.0 → 2.02023-10-26

2.0 → 3.02026-02-26

PHP version history (2 changes)2.0PHP ^8.0

3.0PHP &gt;=8

### Community

Maintainers

![](https://www.gravatar.com/avatar/63a84b9b680bd78e5ce9e59937330e49e20c065b6d147ba2b0f246087a66aa7f?d=identicon)[reptily](/maintainers/reptily)

---

Top Contributors

[![reptily](https://avatars.githubusercontent.com/u/47777868?v=4)](https://github.com/reptily "reptily (1 commits)")

### Embed Badge

![Health badge](/badges/reptily-async-run/health.svg)

```
[![Health](https://phpackages.com/badges/reptily-async-run/health.svg)](https://phpackages.com/packages/reptily-async-run)
```

###  Alternatives

[mottie/tablesorter

tablesorter (FORK) is a jQuery plugin for turning a standard HTML table with THEAD and TBODY tags into a sortable table without page refreshes. tablesorter can successfully parse and sort many types of data including linked data in a cell.

2.6k223.5k](/packages/mottie-tablesorter)[mehrpadin/superfish

Superfish library for the Drupal Superfish module.

951.3M](/packages/mehrpadin-superfish)[pjkui/kindeditor

Yii2 可以使用的KindEditor富文本编辑器。 KindEditor for Yii2

274.3k](/packages/pjkui-kindeditor)

PHPackages © 2026

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