Download the PHP package utopia-php/async without Composer
On this page you can find all versions of the php package utopia-php/async. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download utopia-php/async
More information about utopia-php/async
Files in utopia-php/async
Package async
Short Description High-performance concurrent + parallel library with Promise and Parallel execution support for PHP.
License MIT
Informations about the package async
Utopia Async
A high-performance async/parallel library for PHP 8.1+ providing Promises/A+ compliant promises and true multi-core parallel execution.
Features
- Promise API - Promises/A+ compliant with
then(),catch(),finally(),await() - Parallel Execution - True multi-core parallelism using threads and processes
- Automatic Adapter Selection - Chooses optimal strategy based on runtime environment
- Closure Support - Execute closures across process boundaries
- Built-in Safety - Timeouts, deadlock detection, memory management
Installation
Requirements
- PHP 8.1+
Optional Extensions (for parallel execution)
| Extension/Package | Required For |
|---|---|
ext-swoole >=6.0 + ZTS |
Swoole Thread adapter (best performance) |
ext-swoole + ext-sockets |
Swoole Process adapter |
ext-parallel + ZTS |
ext-parallel adapter |
amphp/parallel |
Amp adapter |
react/child-process |
React adapter |
Promise API
Promises provide asynchronous operations with coroutine support when Swoole is available, falling back to synchronous execution otherwise.
Basic Usage
Promise Methods
Collection Methods
Parallel API
Parallel execution distributes work across CPU cores using threads or processes.
Basic Usage
Map and ForEach
Pool with Concurrency Control
Lifecycle Management
The default worker pool is automatically cleaned up when the script terminates via register_shutdown_function(). You can also manually shutdown early if needed:
Adapters
The library automatically selects the best adapter based on available extensions.
Promise Adapter Priority
| Priority | Adapter | Runtime | Requirements |
|---|---|---|---|
| 1 | Swoole\Coroutine | Async | ext-swoole |
| 2 | React | Async | react/event-loop |
| 3 | Amp | Async | amphp/amp, revolt/event-loop |
| 4 | Sync | Blocking | None (fallback) |
Parallel Adapter Priority
| Priority | Adapter | Runtime | Requirements |
|---|---|---|---|
| 1 | Swoole\Thread | Multi-threaded | ext-swoole >=6.0 with threads, PHP ZTS |
| 2 | ext-parallel | Multi-threaded | ext-parallel, PHP ZTS |
| 3 | Swoole\Process | Multi-process | ext-swoole, ext-sockets |
| 4 | React | Multi-process | react/child-process, react/event-loop, opis/closure |
| 5 | Amp | Multi-process | amphp/parallel |
| 6 | Sync | Sequential | None (fallback) |
Manual Adapter Selection
Exception Handling
Configuration
Both Parallel and Promise facades expose configurable options via static getter/setter methods.
Parallel Configuration
| Option | Default | Description |
|---|---|---|
MaxSerializedSize |
10 MB | Maximum payload size for task data |
MaxTaskTimeoutSeconds |
30s | Task execution timeout |
DeadlockDetectionInterval |
5s | Progress check interval for stuck workers |
MemoryThresholdForGc |
50 MB | GC trigger threshold |
StreamSelectTimeoutUs |
100ms | Non-blocking I/O timeout |
WorkerSleepDurationUs |
10ms | Worker idle sleep duration |
GcCheckInterval |
10 | Tasks between GC checks |
Promise Configuration
| Option | Default | Description |
|---|---|---|
SleepDurationUs |
100μs | Initial sleep for exponential backoff |
MaxSleepDurationUs |
10ms | Maximum sleep duration (backoff cap) |
CoroutineSleepDurationS |
1ms | Coroutine context sleep duration |
Performance
The library achieves significant speedups for CPU-intensive and I/O-bound workloads through true parallel execution.
Running Benchmarks
Benchmarks automatically detect and test all available adapters in your environment:
Adapter Comparison
| Adapter | Type | Best For | Characteristics |
|---|---|---|---|
| Swoole Thread | Multi-threaded | CPU-bound tasks | Lowest overhead, shared memory |
| Swoole Process | Multi-process | I/O-bound tasks | Full isolation, blocking ops |
| Amp | Multi-process | Async I/O | Event-loop based, fibers |
| React | Multi-process | Async I/O | Event-loop based |
| ext-parallel | Multi-threaded | CPU-bound tasks | Native PHP threads |
| Sync | Sequential | Fallback | Always available |
Benchmark Results
Results from running benchmarks with --iterations=10 --load=75 on an 8-core system:
CPU-Intensive Workloads (8 tasks)
| Benchmark | Sync | Swoole Thread | Swoole Process | Amp | React |
|---|---|---|---|---|---|
| Prime calculation (300k) | 0.608s | 0.096s (6.4x) | 0.093s (6.6x) | 0.095s (6.4x) | 0.146s (4.2x) |
| Matrix multiply (300x300) | 2.957s | 0.655s (4.5x) | 0.494s (6.0x) | 0.504s (5.9x) | 0.921s (3.2x) |
I/O-Simulated Workloads (8 tasks)
| Benchmark | Sync | Swoole Thread | Swoole Process | Amp | React |
|---|---|---|---|---|---|
| Sleep tasks (75ms each) | 0.609s | 0.089s (6.8x) | 0.089s (6.9x) | 0.091s (6.7x) | 0.103s (5.9x) |
| Mixed workload | 0.367s | 0.119s (3.1x) | 0.079s (4.6x) | 0.087s (4.2x) | 0.105s (3.5x) |
ext-parallel Benchmark Results (4-core system)
| Benchmark | Sync | ext-parallel | Amp | React |
|---|---|---|---|---|
| Prime calculation (300k) | 0.307s | 0.082s (3.8x) | 0.087s (3.5x) | 0.098s (3.1x) |
| Matrix multiply (300x300) | 1.453s | 0.386s (3.8x) | 0.380s (3.8x) | 0.394s (3.7x) |
| Sleep tasks (75ms each) | 0.305s | 0.084s (3.7x) | 0.087s (3.5x) | 0.106s (2.9x) |
Key Findings
- Swoole Process achieves the best overall performance with up to 6.9x speedup on I/O workloads
- Swoole Thread and Amp show comparable performance on CPU-intensive tasks (~6x speedup)
- ext-parallel excels at CPU-bound tasks, achieving 3.8x speedup on a 4-core system
- React provides solid multi-process execution but has higher overhead than other adapters
- Parallelism overhead means single-task workloads may be slower than sequential execution
- Results vary by environment - run benchmarks to find the best adapter for your use case
Development
License
MIT License. See LICENSE for details.