globus-studio/async
Composer 安装命令:
composer require globus-studio/async
包简介
Promise-style asynchronous programming for PHP, built on Revolt fibers and the event loop.
README 文档
README
Promise-style asynchronous programming for PHP, built on top of Revolt
fibers and the event loop. Concurrency without pthreads, without
pcntl_fork, and without blocking sleep().
Features
async/awaitsemantics on top of native PHP fibers.- Non-blocking
delay()powered by Revolt's event loop. - Promise combinators:
Async::all(),Async::race(),Async::any(). - Timeouts via
timeout()that schedules a real loop timer. - Cooperative cancellation with a dedicated
CancelledException. - Event hooks:
then,catch,finally,progress, plus arbitrary custom events throughon(). - Priority queue via
AsyncManagerfor batched work. - PSR-4, strict types, fully tested with PHPUnit.
Requirements
- PHP 8.1 or newer (Fibers).
revolt/event-loop^1.0.
Installation
composer require globus-studio/async
Quick start
<?php require __DIR__ . '/vendor/autoload.php'; use GlobusStudio\Async\Async; use function GlobusStudio\Async\{async, await, delay}; $task = async(function (Async $self): string { $self->progress(50); delay(0.5); // non-blocking sleep $self->progress(100); return 'done'; }); $task->onProgress(fn ($pct) => printf("progress: %d%%\n", $pct)); echo await($task), PHP_EOL;
Concurrency
use GlobusStudio\Async\Async; use function GlobusStudio\Async\delay; $results = Async::all([ 'a' => Async::run(function () { delay(0.5); return 'A'; }), 'b' => Async::run(function () { delay(0.5); return 'B'; }), 'c' => Async::run(function () { delay(0.5); return 'C'; }), ]); // All three finish in ~0.5s, not 1.5s.
race() returns the first settled task and cancels the rest. any() returns
the first fulfilled task, throwing only if every task rejects.
Timeouts and cancellation
$slow = new Async(function () { delay(10); return 'never'; }); try { $slow->timeout(0.25)->await(); } catch (\GlobusStudio\Async\Exception\TimeoutException $e) { // Operation timed out after 0.250s. }
Cancellation is cooperative. Calling cancel() rejects the task with
CancelledException and emits a cancel event, but does not forcibly
abort the underlying fiber. User code should observe cancellation at
suspension points.
Error handling
Async::run(function () { throw new RuntimeException('nope'); }) ->catch(fn (\Throwable $e) => error_log($e->getMessage())) ->finally(fn () => print "cleanup\n");
Listeners attached after a task settles are still invoked (asynchronously, on the next loop tick) so that ordering does not matter.
Priority queue
use GlobusStudio\Async\AsyncManager; (new Async($high))->setPriority(10); (new Async($low))->setPriority(1); $results = AsyncManager::run(); // high-priority task starts first
API reference
Async
| Method | Description |
|---|---|
__construct(callable $cb) |
Create a task. $cb receives the Async instance. |
static run(callable $cb): self |
Create and immediately start. |
start(): self |
Schedule the task on the event loop. Idempotent. |
await(): mixed |
Suspend until settled; rethrows on rejection. |
then(callable $ok, ?callable $err = null): self |
|
catch(callable $err): self |
|
finally(callable $cb): self |
|
on(string $event, callable $l): self |
Generic event subscription. |
onProgress(callable $cb): self / progress(mixed $data) |
Progress channel. |
timeout(float $seconds): self |
Reject with TimeoutException if not settled in time. |
cancel(): void |
Reject with CancelledException. |
getState(): string / isPending(): bool / isSettled(): bool |
State inspection. |
setPriority(int $p): self |
Enqueue in AsyncManager. |
static all(array $tasks): array |
Concurrent join, preserves keys. |
static race(array $tasks): mixed |
First to settle wins. |
static any(array $tasks): mixed |
First to fulfill wins. |
AsyncManager
| Method | Description |
|---|---|
static addTask(Async $t, int $priority = 0): void |
|
static run(): array |
Drain the queue, returning per-task results (or Throwable on failure). |
static runTasks(): void |
Alias of run() for backwards compatibility. |
static cancelAllOtherTasks(Async $keep): void |
|
static clear(): void / count(): int |
Helpers (GlobusStudio\Async namespace)
async(callable $cb): Asyncawait(Async $task): mixeddelay(float $seconds): void
Running the test suite
composer install
composer test
The suite covers fulfillment, rejection, timeouts, cancellation, concurrency
guarantees of all/race/any, listener ordering, and AsyncManager
prioritisation.
License
MIT. See LICENSE.
globus-studio/async 适用场景与选型建议
globus-studio/async 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 27 次下载、GitHub Stars 达 22, 最近一次更新时间为 2026 年 04 月 25 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「event-loop」 「async」 「concurrency」 「promise」 「await」 「fiber」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 globus-studio/async 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 globus-studio/async 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 globus-studio/async 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Tool for managing fast both asynchronous and multi-threaded execution of tasks. Focused on performance and pleasant CLI interface.
The bundle for easy using json-rpc api on your project
Wrapper around proc_open to ease communication via streams between processes
An async event for hyperf.
An asynchronous event driven PHP framework for easily building fast, scalable network applications.
Pleasantly work with asynchronous code.
统计信息
- 总下载量: 27
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 23
- 点击次数: 48
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-04-25