PHPackages                             superrb/async - 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. [Queues &amp; Workers](/categories/queues)
4. /
5. superrb/async

ActiveLibrary[Queues &amp; Workers](/categories/queues)

superrb/async
=============

A PHP library for handling asynchronous processes and inter-process communication via sockets

v1.0.1(3y ago)88532[1 issues](https://github.com/superrbstudio/async/issues)[1 PRs](https://github.com/superrbstudio/async/pulls)MITPHP

Since Aug 31Pushed 3y ago4 watchersCompare

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

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

Async
=====

[](#async)

A simple PHP library for creating asynchronous processes, and handling inter-process communication via sockets.

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

[](#installation)

```
composer require superrb/async
```

Usage
-----

[](#usage)

### Getting Started

[](#getting-started)

The easiest way to use this package is to use the `async` helper. This simply runs the supplied closure asynchronously within a forked process. The closure must return a boolean to indicate success/failure of the subprocess, and have the return type `bool`, as this is used to set the exit state of the process.

```
echo 1."\n";

async(function(): bool {
	sleep(1);
	echo 2."\n";
	return true;
});

echo 3."\n";

// Output
1
3
2
```

Any arguments passed to `async` will be passed straight to the forked process.

```
async(function($i, $j): bool {
	echo $i."\n";
	echo $j."\n";
	return true;
}, 1, 2);

// Output
1
2
```

### Reusable closures

[](#reusable-closures)

You can reuse a closure, by manually constructing an instance of `Superrb\Async\Handler`

```
$handler = new Superrb\Async\Handler(function(int $i): bool {
	echo $i;
	return true;
});

for ($i = 0; $i < 10; $i++) {
	$handler->run($i);
}

// Processing will pause here until all asynchronous processes
// have completed. $success is true if all processes returned
// true, otherwise it is false
$success = $handler->waitAll();
```

You can run synchronous code within forked processes by passing `false` as the second argument to the `Handler` constructor. This is useful for running long processes such as imports, as any memory consumed within the loop is dumped at the end of the process.

```
$handler = new Superrb\Async\Handler(function(int $i): bool {
	echo $i;
	return true;
}, false);

for ($i = 0; $i < 10; $i++) {
	// The loop will pause whilst the process runs, and continue
	// when it is completed. $success is the return value of the
	// closure
	$success = $handler->run($i);
}
```

### Inter-process communication

[](#inter-process-communication)

#### Sending messages to the parent

[](#sending-messages-to-the-parent)

`async` uses channels and socket pairs to allow for communication between the child and parent process. Communication is one way - you can pass messages from the child process back to the main process using `$this->send()` within your closure. Messages can be strings, objects or arrays.

```
$handler = new Superrb\Async\Handler(function(int $i): bool {
	$this->send('Hi from process '.$i);
	return true;
});

for ($i = 0; $i < 10; $i++) {
	$handler->run($i);
}

$handler->waitAll();
```

#### Reading messages

[](#reading-messages)

Messages can then be read by calling the generator `$handler->getMessages()` once processing has completed. For asynchronous processes, the messages will always be received **in the order the handlers were run** regardless of which process finished first.

```
foreach($handler->getMessages() as $message) {
	echo $message."\n";
}

// output
Hi from process 1
Hi from process 2
Hi from process 3
...
```

For synchronous processes, you can read messages at the end of each process. If you do this, you'll need to clear the message pool manually after reading them.

```
for ($i = 0; $i < 10; $i++) {
	$handler->run($i);

	foreach($handler->getMessages() as $message) {
		echo $message."\n";
	}

	$handler->clearMessages();
}
```

#### Increasing the communication buffer

[](#increasing-the-communication-buffer)

By default messages are sent within a 1024-byte message buffer, and attempting to send data larger than this will result in an exception. You can increase/decrease the buffer by calling `setMessageBuffer` on the handler.

```
$handler->setMessageBuffer(4096);
```

Contributing
------------

[](#contributing)

All contributions are welcome, and encouraged. Please read our [contribution guidelines](CONTRIBUTING.md) and [code of conduct](CODE-OF-CONDUCT.md) for more information.

License
-------

[](#license)

Copyright (c) 2017 [Superrb Studio](https://superrb.com)

Async is licensed under The MIT License (MIT)

Team
----

[](#team)

- James Dinsdale (@molovo)

###  Health Score

33

—

LowBetter than 75% of packages

Maintenance18

Infrequent updates — may be unmaintained

Popularity23

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity66

Established project with proven stability

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

Total

4

Last Release

1453d ago

Major Versions

v0.2.0 → v1.0.02022-05-25

### Community

Maintainers

![](https://www.gravatar.com/avatar/790a48c33c76e344f6eab8edd15abca37ad75d2a957e744347b5ac4acecd9ec6?d=identicon)[molovo](/maintainers/molovo)

---

Top Contributors

[![molovo](https://avatars.githubusercontent.com/u/2746289?v=4)](https://github.com/molovo "molovo (15 commits)")

---

Tags

asyncasynchronousforkphpphp-librarysocketsocket-communicationsubprocessthreadsasyncasynchronousSocketforkthreadssubprocesssocket-communication

### Embed Badge

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

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

###  Alternatives

[amphp/amp

A non-blocking concurrency framework for PHP applications.

4.4k123.4M323](/packages/amphp-amp)[react/socket

Async, streaming plaintext TCP/IP and secure TLS socket server and client connections for ReactPHP

1.3k116.9M402](/packages/react-socket)[revolt/event-loop

Rock-solid event loop for concurrent PHP applications.

92343.6M138](/packages/revolt-event-loop)[amphp/parallel

Parallel processing component for Amp.

85046.2M74](/packages/amphp-parallel)[icicleio/icicle

Icicle is a PHP library for writing asynchronous code using synchronous coding techniques.

1.1k150.9k14](/packages/icicleio-icicle)[hollodotme/fast-cgi-client

A PHP fast CGI client to send requests (a)synchronously to PHP-FPM.

56423.9M25](/packages/hollodotme-fast-cgi-client)

PHPackages © 2026

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