Download the PHP package venndev/vosaka-fourotines without Composer
On this page you can find all versions of the php package venndev/vosaka-fourotines. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download venndev/vosaka-fourotines
More information about venndev/vosaka-fourotines
Files in venndev/vosaka-fourotines
Package vosaka-fourotines
Short Description Structured async programming for PHP using Fibers, inspired by Kotlin Coroutines. Features AsyncIO (non-blocking streams), ForkProcess (low-overhead child processes), Flow/SharedFlow/StateFlow with backpressure, Channels, Mutex, and cooperative scheduling. Can integrate with VOsaka.
License LGPL-2.1-only
Informations about the package vosaka-fourotines
VOsaka Foroutines
A PHP library for structured asynchronous programming using foroutines (fiber + coroutines), inspired by Kotlin coroutines. This is project with the contribution of a project from php-async
π Documentation
New to VOsaka Foroutines? Check out our Structured Documentation (following the DiΓ‘taxis framework), which includes:
- Tutorials: Step-by-step learning lessons.
- How-to Guides: Task-oriented recipes for common problems.
- Reference: Detailed technical descriptions of the API.
- Explanation: Conceptual overviews and architectural deep-dives.
Architecture
Features
Core β RunBlocking, Launch, Async, Async::awaitAll(), Delay, Repeat, WithTimeout, Job lifecycle
Dispatchers β DEFAULT (fibers + AsyncIO), IO (child process via WorkerPool), MAIN (event loop)
WorkerPool β Pre-spawned long-lived worker processes with task batching, dynamic pool sizing, and respawn backoff
FiberPool β Reusable Fiber instances for scheduler optimization (default: 10, dynamic sizing)
Channel β Four transports: in-process, socket pool (default), socket per-channel, file-based
AsyncIO β Non-blocking stream I/O via stream_select() (TCP, TLS, HTTP, files, DNS)
Flow β Cold Flow, SharedFlow, StateFlow with backpressure (SUSPEND, DROP_OLDEST, DROP_LATEST, ERROR)
Actor Model β Message-passing concurrency with Channel-based mailboxes and ActorSystem registry
Supervisor Tree β OTP-style supervision with ONE_FOR_ONE, ONE_FOR_ALL, REST_FOR_ONE strategies
Sync β Mutex (file, semaphore, APCu), Select for channel multiplexing
Rules
Requirements
- PHP 8.2+
- ext-shmop, ext-fileinfo, ext-zlib
| Optional Extension | Purpose |
|---|---|
| ext-pcntl | Low-overhead IO dispatch via pcntl_fork() (~1-5ms vs ~50-200ms) |
| ext-sysvsem | Semaphore-based Mutex |
| ext-apcu | APCu-based Mutex |
Installation
Usage
All entry points must be wrapped in main() or use the #[AsyncMain] attribute:
RunBlocking + Launch
Async / Await
Async::awaitAll β Concurrent Awaiting
awaitAll() drives multiple async tasks forward simultaneously, returning all results in order. This is significantly more efficient than awaiting sequentially.
WithTimeout
Job Lifecycle
Channel
| Mode | Factory | Use Case |
|---|---|---|
| In-process | Channel::new(capacity) |
Fibers in the same process |
| Socket pool (default) | Channel::create(capacity) |
IPC via shared ChannelBrokerPool |
| Socket per-channel | Channel::newSocketInterProcess(name, capacity) |
Legacy β 1 process per channel |
| File-based | Channel::newInterProcess(name, capacity) |
IPC via temp file + mutex |
Non-blocking operations:
Channels utility class:
Select
Flow
AsyncIO β Non-blocking Stream I/O
All methods return Deferred β a lazy wrapper that executes on ->await():
| Method | Returns | Description |
|---|---|---|
tcpConnect(host, port)->await() |
resource |
Non-blocking TCP connection |
tlsConnect(host, port)->await() |
resource |
Non-blocking TLS/SSL connection |
streamRead(stream, maxBytes)->await() |
string |
Read up to N bytes |
streamReadAll(stream)->await() |
string |
Read until EOF |
streamWrite(stream, data)->await() |
int |
Write data |
httpGet(url)->await() |
string |
HTTP GET |
httpPost(url, body)->await() |
string |
HTTP POST |
fileGetContents(path)->await() |
string |
Read entire file |
filePutContents(path, data)->await() |
int |
Write file |
dnsResolve(hostname)->await() |
string |
Resolve hostname to IP |
Mutex
Dispatchers
| Dispatcher | Description |
|---|---|
DEFAULT |
Runs in the current fiber context (+ AsyncIO for non-blocking streams) |
IO |
Offloads to a worker process via WorkerPool |
MAIN |
Schedules on the main event loop |
Thread::await()
While RunBlocking automatically drains all pending tasks before returning, Thread::await() allows you to manually block and drive the event loop until all work (Launch jobs, WorkerPool tasks, and AsyncIO) is finished.
When do you need it?
- Inside
RunBlocking: If you want to ensure all background tasks (likeLaunchjobs) are completed before proceeding to the next line of code within the sameRunBlockingblock. - Outside
RunBlocking: When you are usingAsyncMainormain()and have scheduled tasks that need to be completed before the script exits, but you aren't using a blocking runner.
WorkerPool
A pool of pre-spawned long-lived child processes. On Linux/macOS uses pcntl_fork() + Unix socket pairs; on Windows uses proc_open() + TCP loopback sockets.
Task Batching
When many small tasks are submitted, IPC round-trip overhead dominates. Task batching groups multiple tasks into a single message sent to each worker, dramatically reducing round-trips.
| Batch Size | Behavior |
|---|---|
| 1 (default) | Original single-task protocol β lowest latency per task |
| 5β10 | Good balance for many small/fast tasks |
| 20β50 | Maximum throughput for trivial tasks |
Batching is fully backward compatible β when batchSize=1, the pool uses the original TASK:/RESULT: protocol.
Dynamic Pool Sizing
The pool can automatically scale between a minimum and maximum number of workers based on workload pressure.
Scale-up: When all workers are busy and tasks are queued, a new worker is spawned (up to maxPoolSize).
Scale-down: When a worker has been idle longer than idleTimeout and the pool exceeds minPoolSize, it is shut down.
When dynamic scaling is disabled (default), the pool behaves exactly as before β a fixed number of workers.
Worker Respawn Backoff
When a worker crashes repetitively, respawning uses exponential backoff (100ms β 200ms β β¦ max 30s) to prevent CPU spin. After 10 consecutive failures, the worker slot is removed (circuit-breaker).
FiberPool
Reusable Fiber instances to reduce allocation overhead. Integrated into Launch, Async, RunBlocking.
Actor Model
Supervisor Tree
OTP-style supervision with automatic restart on child failure.
| Strategy | Behavior |
|---|---|
ONE_FOR_ONE |
Restart only the crashed child |
ONE_FOR_ALL |
Restart all children |
REST_FOR_ONE |
Restart crashed child + all started after it |
ForkProcess
On Linux/macOS, ForkProcess creates child processes by forking the current process instead of spawning a new interpreter:
| Strategy | Overhead | Closure Serialization |
|---|---|---|
ForkProcess (pcntl_fork) |
~1-5ms | Not needed (memory copied) |
Process (symfony/process) |
~50-200ms | Required |
Selection is automatic β Worker uses fork when available, falls back to symfony/process on Windows.
Platform Support
| Feature | Linux/macOS | Windows |
|---|---|---|
| Fibers (core) | β | β |
| FiberPool | β | β |
| AsyncIO (stream_select) | β | β |
| Channel (all transports) | β | β |
| Actor Model | β | β |
| Supervisor Tree | β | β |
| WorkerPool (fork mode) | β | β (uses socket mode) |
| WorkerPool (socket mode) | β | β |
| ForkProcess (pcntl_fork) | β | β (fallback to symfony/process) |
| Mutex (file lock) | β | β |
| Mutex (semaphore) | β (ext-sysvsem) | β |
| Mutex (APCu) | β (ext-apcu) | β (ext-apcu) |
Comparison with JavaScript Async
| Aspect | Node.js | VOsaka Foroutines |
|---|---|---|
| Runtime | libuv event loop (C) | PHP Fibers + stream_select |
| I/O model | Non-blocking by default | AsyncIO for streams; Dispatchers::IO for blocking APIs |
| Concurrency | Single-threaded + worker threads | Single process + child processes (fork/spawn) |
| Syntax | async/await (language-level) |
Async::new()->await() / Async::awaitAll() (library-level) |
| Worker pool | worker_threads |
WorkerPool with task batching + dynamic scaling |
| IPC channels | MessagePort |
Channel::create() (shared TCP pool) |
| Flow control | Node.js Streams | BackpressureStrategy (SUSPEND/DROP/ERROR) |
License
GNU Lesser General Public License v2.1
All versions of vosaka-fourotines with dependencies
symfony/process Version ^7.3
laravel/serializable-closure Version ^2.0
ext-shmop Version *
ext-fileinfo Version *
ext-zlib Version *