Completely refactor to enable a fully non-blocking, event-loop driven architecture #3
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Purpose
This PR introduces a complete overhaul of the Matrix library’s asynchronous execution model. Previously, Matrix relied on Fibers for concurrency, but this proved challenging and introduced complexity in error handling and interleaving tasks. Now, we have shifted to using
pcntl_fork()
and ReactPHP promises to achieve true parallelism and a simpler, promise-like API—making asynchronous task handling more intuitive, JavaScript-like, and robust.For reference, this change addresses multiple issues related to confusing Fiber usage, lack of proper concurrency, and difficulties in error propagation. The new model ensures tasks actually run concurrently in separate processes, returning results asynchronously without blocking the main script.
Approach
pcntl_fork()
: The asynchronous execution now happens in child processes, allowing true parallel execution and simpler architecture..then()
/.catch()
promise style.AsyncProcessManager
&AsyncPromise
: IntroducesAsyncProcessManager
for forking and communicating with child processes, andAsyncPromise
for a promise-like API.async()
helper: Simplifies the user experience, providing a direct function to start async tasks and attach handlers, closely mirroring JavaScript’s async/await feel.Open Questions and Pre-Merge TODOs
3.0.0
to reflect the breaking changes.Learning
Research included exploring ReactPHP’s event loop and promise design patterns, revisiting
pcntl_fork()
usage, and studying best practices for inter-process communication in PHP. We also examined how other asynchronous PHP libraries structure their concurrency models, leading us to adopt a more conventional process-based approach rather than Fibers. This has simplified error handling, provided clearer concurrency guarantees, and made the user-facing API more intuitive.