PHPackages                             baiy/think-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. baiy/think-async

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

baiy/think-async
================

thinkphp 异步代码执行/异步延迟执行/异步事件订阅

1.0.1(4y ago)218.2k1[1 issues](https://github.com/baiy/think-async/issues)MITPHP

Since Sep 16Pushed 4y ago1 watchersCompare

[ Source](https://github.com/baiy/think-async)[ Packagist](https://packagist.org/packages/baiy/think-async)[ Docs](https://github.com/baiy/think-async)[ RSS](/packages/baiy-think-async/feed)WikiDiscussions master Synced 3w ago

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

think-async for ThinkPHP 6
==========================

[](#think-async-for-thinkphp-6)

- 提供 [ThinkPHP](https://github.com/top-think/think) 项目内部快速实现的`异步代码执行`/`异步延迟执行`/`异步事件订阅`功能
- 内部由 [top-think/think-queue](https://github.com/top-think/think-queue) 提供异步队列支持
- 执行流程: 调用相关方法将需要异步执行的代码插入相应队列中,使用`top-think/think-queue`提供的常驻监听脚本执行对应的代码,来实现系统的异步化

安装
--

[](#安装)

```
composer require baiy/think-async

```

配置
--

[](#配置)

```
config/async.php

```

代码异步执行
------

[](#代码异步执行)

```
use Baiy\ThinkAsync\Facade\Async;
// 异步执行代码
Async::exec($className,$methodName,...$params);
// 异步执行代码使用自定义队列
Async::execUseCustomQueue($className,$methodName,$queue,...$params);
// 异步延迟执行代码
Async::delay($delay,$className,$methodName,...$params);
// 异步延迟执行代码使用自定义队列
Async::delayUseCustomQueue($delay,$className,$methodName,$queue,...$params);
```

### 例子

[](#例子)

```
namespace app\controller;

use Baiy\ThinkAsync\Facade\Async;
use app\BaseController;
use think\facade\Log;

class Index extends BaseController
{
    public function index()
    {
        // 异步执行
        Async::exec(self::class, 'test', 'exec');
        Async::execUseCustomQueue(self::class, 'test','async_exec_method_custom', 'exec');
        // 异步延迟执行 延迟20秒
        Async::delay(20, self::class, 'test', 'delay');
        Async::delayUseCustomQueue(20, self::class, 'test','async_exec_method_custom', 'delay');
        return '';
    }

    public static function test($type)
    {
        Log::info("异步执行的方法 {$type}");
    }
}
```

事件订阅
----

[](#事件订阅)

```
use Baiy\ThinkAsync\Facade\Async;
// 事件触发
Async::trigger($name,...$params);
```

### 事件订阅配置

[](#事件订阅配置)

默认使用`config/async.php`配置文件中`subscribe_event_config`进行配置,可使用`subscribe_event_get_class`来定制化配置来源

内部日志拦截
------

[](#内部日志拦截)

> 可选操作, 不设置默认使用系统`\think\Log`方法进行日志记录

```
use Baiy\ThinkAsync\Facade\Async;
use Psr\Log\LoggerInterface;
/** @var LoggerInterface $log */
Async::setLog($log);
```

队列信息
----

[](#队列信息)

```
use Baiy\ThinkAsync\Facade\Async;
// 获取所有队列标示
Async::queue();
// 获取队列长度
Async::queueSize($queue);
// 获取队列名称
Async::queueName($queue);
```

获取常驻脚本命令
--------

[](#获取常驻脚本命令)

```
## 执行下方命令会输出相关的队列监听命令
php think async:show

## echo
======= async_exec_method =======
listen mode:php think queue:listen --queue async_exec_method
work mode:php think queue:work --queue async_exec_method
======= async_delay_method =======
listen mode:php think queue:listen --queue async_delay_method
work mode:php think queue:work --queue async_delay_method
======= async_subscribe_demo =======
listen mode:php think queue:listen --queue async_subscribe_demo
work mode:php think queue:work --queue async_subscribe_demo

```

> `listen`/`work`模式的区别和命令其他配置参数请查阅 [top-think/think-queue](https://github.com/top-think/think-queue) 文档

其他说明
----

[](#其他说明)

1. 异步执行的方法均为静态公共方法(`public static`), 请知晓
2. [top-think/think-queue](https://github.com/top-think/think-queue) 的默认配置是使用`同步模式`来消费队列, 请修改为异步模式

###  Health Score

32

—

LowBetter than 69% of packages

Maintenance19

Infrequent updates — may be unmaintained

Popularity30

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity56

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

Total

2

Last Release

1731d ago

### Community

Maintainers

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

---

Top Contributors

[![baiy](https://avatars.githubusercontent.com/u/2341581?v=4)](https://github.com/baiy "baiy (13 commits)")

---

Tags

asyncqueuesubscribethinkphp

### Embed Badge

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

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

###  Alternatives

[league/geotools

Geo-related tools PHP 7.3+ library

1.4k5.5M29](/packages/league-geotools)[illuminate/bus

The Illuminate Bus package.

6045.5M504](/packages/illuminate-bus)

PHPackages © 2026

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