PHPackages                             chemem/asyncify - 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. chemem/asyncify

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

chemem/asyncify
===============

A simple library with which to run blocking I/O in a non-blocking fashion.

v0.3.1(11mo ago)31247Apache-2.0PHPPHP &gt;=7.2CI passing

Since Aug 5Pushed 11mo ago1 watchersCompare

[ Source](https://github.com/ace411/asyncify)[ Packagist](https://packagist.org/packages/chemem/asyncify)[ RSS](/packages/chemem-asyncify/feed)WikiDiscussions master Synced today

READMEChangelog (6)Dependencies (6)Versions (8)Used By (0)

asyncify
========

[](#asyncify)

[![StyleCI](https://camo.githubusercontent.com/8aa96f3144490b61b9c5716dd788d057d2993b941c9a46beeb343a9ebb955af6/68747470733a2f2f6769746875622e7374796c6563692e696f2f7265706f732f3336353031383034382f736869656c643f6272616e63683d6d6173746572)](https://github.styleci.io/repos/365018048?branch=master)[![asyncify CI](https://github.com/ace411/asyncify/actions/workflows/ci.yml/badge.svg)](https://github.com/ace411/asyncify/actions/workflows/ci.yml)[![License](https://camo.githubusercontent.com/29f32af0e0a249f5be9f7799fa2d6a2a04ae981c34a27a4afa628c963ce8c550/687474703a2f2f706f7365722e707567782e6f72672f6368656d656d2f6173796e636966792f6c6963656e7365)](https://packagist.org/packages/chemem/asyncify)[![Latest Stable Version](https://camo.githubusercontent.com/f34a16cba500baa0c039245515329f4c9f4a38ea2c87276102e54de91e1b3719/687474703a2f2f706f7365722e707567782e6f72672f6368656d656d2f6173796e636966792f76)](https://packagist.org/packages/chemem/asyncify)[![PHP Version Require](https://camo.githubusercontent.com/a13fa10ca9f5359c66b91ab6380e4ed3895520012d1c6002a1e104bc55df51ca/687474703a2f2f706f7365722e707567782e6f72672f6368656d656d2f6173796e636966792f726571756972652f706870)](https://packagist.org/packages/chemem/asyncify)

A simple library with which to run blocking I/O in a non-blocking fashion.

Requirements
------------

[](#requirements)

- PHP 7.2 or newer

Rationale
---------

[](#rationale)

PHP is home to a host of functions that condition CPU idleness between successive (serial) executions—blocking functions. The expense of blocking calls—invocations of such functions—is such that they can, when deployed haphazardly in evented systems, inflict unnecessary CPU waiting behavior whilst the kernel attempts to interleave non-blocking calls. `asyncify` is a bridge between the blocking I/O in the language userspace and the evented I/O in ReactPHP. It allows those who choose to avail themselves of it the ability to run their blocking code, with minimal plumbing, in evented systems, without derailing them.

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

[](#installation)

Though it is possible to clone the repo, Composer remains the best tool for installing `asyncify`. To install the package via Composer, type the following in a console of your choosing.

```
$ composer require chemem/asyncify
```

Newer versions of the library prioritize multithreading. The precondition for operationalizing multithreading is installing the [parallel](https://github.com/krakjoe/parallel) extension (`ext-parallel`) and [`react-parallel/runtime`](https://github.com/reactphp-parallel/runtime) library which can be done with the directives in the snippet below.

```
$ pie install pecl/parallel
$ echo "\nextension=parallel" >> "/path/to/php.ini"
$ composer require react-parallel/runtime
```

Usage
-----

[](#usage)

If you want to take a Functional Programming approach, facilitated by currying, the example below should suffice.

```
use function Chemem\Asyncify\call;

$call = call('file_get_contents', ['foo.txt'])
  ->then(
    function (?string $contents) {
      echo $contents;
    },
    function (\Throwable $err) {
      echo $err->getMessage();
    }
  );
```

### Or

[](#or)

If you prefer a more conventional OOP approach, the snippet below should prove apt.

```
use Chemem\Asyncify\Async;

$exec = Async::create()
  ->call('file_get_contents', ['foo.txt'])
  ->then(
    function (?string $contents) {
      echo $contents;
    },
    function (\Throwable $err) {
      echo $err->getMessage();
    }
  );
```

The examples directory contains more nuanced uses of the library that I recommend you check out.

Limitations
-----------

[](#limitations)

- `asyncify` is no panacea, but is capable of asynchronously executing a plethora of blocking calls. As presently constituted, the library is **incapable of processing inputs and outputs that cannot be serialized**. Its quintessential asynchronous function application primitive - `call()` - works almost exclusively with string encodings of native language functions and lambdas imported via an autoloading mechanism.
- The library, in its default configuration, cannot parse closures. All executable arbitrary code should be emplaced in a string whose sole constituent is an immediately invokable anonymous function the format of which is `(function (...$args) { /* signature */ })`.

Multithreading
--------------

[](#multithreading)

With multithreading enabled, it is possible to invoke closures and other lambdas without necessarily representing them as strings. Although string encodings are still workable, lambdas like closures should be the preferred option for representing arbitrary blocking logic. The code in the following example should work with multithreading enabled.

```
use function Chemem\Asyncify\call;

$exec = call(
  function (...$args) {
    return \file_get_contents(...$args);
  },
  ['/path/to/file']
);

$exec->then(
  function (string $contents) {
    echo $contents;
  },
  function (\Throwable $err) {
    echo $err->getMessage();
  }
);
```

> It must be noted that string representations of lambdas (anonymous functions, closures and such) that are compatible with the default child process configuration, are not usable in versions that support multithreading.

API Reference
-------------

[](#api-reference)

### Object

[](#object)

```
namespace Chemem\Asyncify;

use React\{
  EventLoop\LoopInterface,
  Promise\PromiseInterface,
};

class Async {

  /* Methods */
  public static create( ?string $autoload = null [, ?LoopInterface $rootDir = null ] ) : Async;
  public function call( string|callable $function [, array $args ] ) : PromiseInterface;
}
```

`Async::__construct` - Creates a new Async object instance

`Async::call` - Asynchronously calls a synchronous (blocking) PHP function

### Function

[](#function)

```
namespace Chemem\Asyncify;

use React\{
  EventLoop\LoopInterface,
  Promise\PromiseInterface,
};

call ( string|callable $func [, array $args [, ?string $autoload = null [, ?LoopInterface $args = null ] ] ] ) : PromiseInterface;
```

`call` - Curryied function that bootstraps asynchronous function calls

### Important Considerations

[](#important-considerations)

- `asyncify`, by default, utilizes the autoload file (`autoload.php`) in the `vendor` directory of the composer project in which it resides.
- The library converts all errors in the functions slated for non-blocking execution to exceptions.

Dealing with problems
---------------------

[](#dealing-with-problems)

Endeavor to create an issue on GitHub when the need arises or send an email to .

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

[](#contributing)

Consider buying me a coffee if you appreciate the offerings of the project and/or would like to provide more impetus for me to continue working on it.

[![Buy Me A Coffee](https://camo.githubusercontent.com/5505a36d937780d075baf214225c78c0cea3f1be90cd18062fb6b7b8e43b03bf/68747470733a2f2f63646e2e6275796d6561636f666665652e636f6d2f627574746f6e732f6c61746f2d77686974652e706e67)](https://www.buymeacoffee.com/agiroLoki)

###  Health Score

33

—

LowBetter than 72% of packages

Maintenance52

Moderate activity, may be stable

Popularity21

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity41

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

Recently: every ~225 days

Total

7

Last Release

332d ago

### Community

Maintainers

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

---

Top Contributors

[![ace411](https://avatars.githubusercontent.com/u/11040337?v=4)](https://github.com/ace411 "ace411 (40 commits)")

###  Code Quality

TestsPHPUnit

Code StylePHP CS Fixer

### Embed Badge

![Health badge](/badges/chemem-asyncify/health.svg)

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

###  Alternatives

[composer/composer

Composer helps you declare, manage and install dependencies of PHP projects. It ensures you have the right stack everywhere.

29.5k196.2M3.1k](/packages/composer-composer)[friendsofphp/php-cs-fixer

A tool to automatically fix PHP code style

13.5k251.2M25.1k](/packages/friendsofphp-php-cs-fixer)[ccxt/ccxt

A cryptocurrency trading API with more than 100 exchanges in JavaScript / TypeScript / Python / C# / PHP / Go

43.2k341.0k1](/packages/ccxt-ccxt)[team-reflex/discord-php

An unofficial API to interact with the voice and text service Discord.

1.1k420.9k26](/packages/team-reflex-discord-php)[internal/dload

Downloads binaries.

102187.3k19](/packages/internal-dload)[rector/rector-src

Instant Upgrade and Automated Refactoring of any PHP code

136406.3k14](/packages/rector-rector-src)

PHPackages © 2026

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