How spatie/async works
Main Components
• Pool: This is the core component that manages the queue of processes/tasks, limits concurrency, and coordinates process execution (both asynchronous and synchronous modes).
• Tasks & Closures: Represented by the Task class and invokable classes. These encapsulate the work that can be run in separate processes.
• Process Components:
• ParallelProcess and SynchronousProcess in the “Process” folder show different execution modes.
• ProcessCallbacks provide hooks (then, catch, timeout) that handle outcomes of individual tasks.
• Runtime: ChildRuntime and ParentRuntime classes in the “Runtime” directory indicate the bifurcation between the main (parent) process and the spawned child processes.
• Error Handling & Output: Under the “Output” folder you see error/exception classes (e.g., ParallelError, ParallelException, SerializableException) that encapsulate how errors and outputs are communicated back to the parent process.
• Helper Functions: Found in the helper file, these provide utility functions (like async/await) that simplify managing parallel tasks.
Relationships and Interactions
• The Pool acts as the orchestrator. It receives tasks (as closures, invokable objects, or Task instances) and dispatches them to child processes, creating a relationship between Pool and Process components.
• Each task executed via Pool is internally wrapped by a Process (either ParallelProcess or SynchronousProcess) which handles process creation and communication.
Architectural Patterns and Design Principles
• The architecture follows a clear separation of concerns. The Pool coordinates and schedules tasks, while Process and Runtime components handle execution.
• The implementation uses event-driven callbacks (“then”, “catch”, “timeout”) that allow flexible handling of asynchronous events.
• There is an evident fork/signal-handling mechanism based on PHP’s PCNTL capabilities and the Symfony Process component (which is mentioned in the README).
• Although not a full-blown MVC or microservices pattern, the library’s design is modular, and each folder (Process, Output, Runtime) clearly delineates responsibility.
Relevant Technologies and Frameworks
• PHP’s PCNTL and POSIX extensions (for process management and signal handling)
• Symfony Process (used under the hood to manage subprocesses)
• Composer for dependency management (as seen in composer.json)
• PHPUnit (for testing, as seen in the tests directory)
Summarizing the System Architecture
• The developer’s interface: API for creating pools and adding tasks.
• The orchestration layer (Pool) that handles parallel processing.
• The core execution layers (Process and Runtime) that manage child processes.
• The error and output handling interface (Output classes) enabling graceful response to process results and failures.
• That all components are detailed so that one can see how asynchronous operations are managed from submission to completion.