- What is spatie/fork and how does it work?
- spatie/fork is a lightweight PHP package for running code concurrently by forking the main PHP process into child processes. You define multiple closures, and the package executes them in parallel while collecting results in the original order. It is designed for Unix-like CLI environments and uses the pcntl and posix extensions.
- Does spatie/fork work with Laravel?
- Yes, spatie/fork can be used in Laravel applications because it is framework-agnostic, but there is no native Laravel integration. It fits best inside Laravel console commands, queue jobs, or batch-processing workflows where CLI execution is available. You must handle process cleanup, shared state, and observability yourself.
- What PHP version and extensions does spatie/fork require?
- spatie/fork requires PHP 8 and the pcntl extension, with the posix extension needed for correct process handling. These extensions are commonly available on Unix-like systems such as Linux and macOS. The package has minimal dependencies beyond PHP core extensions.
- Can I use spatie/fork in a Laravel web request or HTTP route?
- No, spatie/fork relies on pcntl, which only works in CLI processes and not in a web context. You should use it from Laravel console commands, queue workers, or scheduled tasks rather than HTTP routes. For web-triggered work, dispatch a queued job that runs in a CLI worker.
- How do I install spatie/fork with Composer?
- Install spatie/fork by running composer require spatie/fork in your project. After installation, you can import Spatie\Fork\Fork and start defining closures to run concurrently. Confirm that your CLI PHP binary has the pcntl and posix extensions enabled.
- How do I run multiple closures concurrently and collect results in order?
- Create a Fork instance with Fork::new() and pass multiple closures to the run method. The closures execute in parallel child processes, and the returned results array preserves the order of the closures you provided. This makes it straightforward to map parallel results back to their original tasks.
- Is spatie/fork suitable for CPU-bound or I/O-bound tasks?
- spatie/fork can handle both CPU-bound and I/O-bound workloads, such as heavy computations, image processing, API scraping, or batch file operations. For I/O-bound work, compare it with Laravel queues backed by Redis or async PHP solutions, since queues may offer better retryability and distribution. For CPU-bound work, process forking can provide useful parallelism on fork-friendly hosts.
- How many concurrent forks can I safely run with spatie/fork?
- There is no universal safe number because each forked process can consume roughly 10 to 50 MB of RAM or more depending on your application. Start with two to four forks in a pilot command and monitor memory, CPU, and any ulimit restrictions on the host. Scale gradually and isolate heavy tasks to avoid exhausting resources.
- How do I handle failed child processes, timeouts, and zombie processes with spatie/fork?
- You should add explicit process management around forked tasks, including signal handling for graceful shutdowns and pcntl_waitpid to reap child processes and avoid zombies. For retries and timeout policies, wrapping forked work in Laravel queue jobs can provide more robust failure handling. Log child-process failures clearly because debugging forked processes can be difficult.
- Does spatie/fork work on Windows?
- No, spatie/fork requires pcntl and posix, which are available on Unix-like systems such as Linux and macOS rather than native Windows PHP. Windows users can use WSL or choose an alternative concurrency approach. The package is not a good fit for native Windows environments.