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

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

reactphp-x/asyncify
===================

v2.0.0(1y ago)051MITPHP

Since Aug 10Pushed 1y agoCompare

[ Source](https://github.com/reactphp-x/asyncify)[ Packagist](https://packagist.org/packages/reactphp-x/asyncify)[ RSS](/packages/reactphp-x-asyncify/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (2)Dependencies (1)Versions (3)Used By (1)

Asyncify
========

[](#asyncify)

一个基于ReactPHP的异步处理库，可以轻松地将同步代码转换为异步执行。

特性
--

[](#特性)

- 简单易用的API
- 支持Promise和Stream两种异步模式
- 基于进程池的异步执行
- 自动管理进程生命周期

安装
--

[](#安装)

```
composer require reactphp-x/asyncify
```

基本用法
----

[](#基本用法)

### 1. 异步执行并返回Promise

[](#1-异步执行并返回promise)

```
use ReactphpX\Asyncify\Asyncify;

// 初始化进程池（可选）
Asyncify::init(1, 1); // 最小1个进程，最大1个进程

// 异步执行并获取结果
Asyncify::call(function () {
    return json_encode(['hello' => 'world']);
})->then(function ($data) {
    var_dump($data);
}, function ($e) {
    var_dump($e->getMessage());
});
```

### 2. 异步读取文件

[](#2-异步读取文件)

```
Asyncify::call(function () {
    return file_get_contents('path/to/file.txt');
})->then(function ($data) {
    var_dump($data);
});
```

### 3. 流式处理

[](#3-流式处理)

```
$stream = Asyncify::call(function ($stream) {
    $i = 0;
    $timer = Loop::addPeriodicTimer(1, function () use ($stream, &$i) {
        $i++;
        $stream->write('hello world:'. $i);
    });

    Loop::addTimer(5, function () use ($timer, $stream) {
        Loop::cancelTimer($timer);
        $stream->end();
    });
    return $stream;
}, true);

$stream->on('data', function ($data) {
    var_dump($data);
});
```

API
---

[](#api)

### Asyncify::init($min, $max)

[](#asyncifyinitmin-max)

初始化进程池

- `$min`: 最小进程数
- `$max`: 最大进程数

### Asyncify::call(callable $callable, $isStream = false, $prioritize = 0)

[](#asyncifycallcallable-callable-isstream--false-prioritize--0)

异步执行一个函数

- `$callable`: 要执行的函数
- `$isStream`: 是否以流的方式返回结果
- `$prioritize`: 优先级

返回：

- 当`$isStream = false`时，返回Promise
- 当`$isStream = true`时，返回Stream

注意事项
----

[](#注意事项)

1. 确保在使用前正确安装并配置ReactPHP相关依赖
2. 异步执行的函数中不能访问外部变量的引用
3. 对于长时间运行的任务，建议使用流式处理

许可证
---

[](#许可证)

MIT

###  Health Score

26

—

LowBetter than 43% of packages

Maintenance46

Moderate activity, may be stable

Popularity4

Limited adoption so far

Community8

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

Total

2

Last Release

417d ago

Major Versions

v1.0.0 → v2.0.02025-03-28

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/76907477?v=4)[wpjscc](/maintainers/wpjscc)[@wpjscc](https://github.com/wpjscc)

---

Top Contributors

[![wpjscc](https://avatars.githubusercontent.com/u/76907477?v=4)](https://github.com/wpjscc "wpjscc (10 commits)")

### Embed Badge

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

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

PHPackages © 2026

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