Weave Code
Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Fork Laravel Package

spatie/fork

Run PHP code concurrently with spatie/fork, which forks the main process into child tasks. Pass multiple closures to run at the same time, then collect results in order. Requires PHP 8 and the pcntl extension, and works in CLI only, not web contexts.

View on GitHub
Deep Wiki
Context7

spatie/fork is a lightweight package for running PHP code concurrently. It forks the main PHP process into one or more child tasks, letting multiple closures execute at the same time—ideal for slow API calls or independent I/O work. Results are returned in order, so $results[0] corresponds to the first closure passed to run().

  • Simple API — Fork::new()->run(...) accepts any number of closures
  • Ordered results — outputs match the order of the tasks you supplied
  • Zero dependencies — built on PHP's native pcntl extension
  • CLI-focused — requires PHP 8 plus pcntl and posix; not for web contexts
  • Great for I/O — parallelize slow network or disk-bound operations with ease
Frequently asked questions about Fork
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.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
robertmarney/lara-hierarchial-collections
simonschaufi/rule-doc-generator
emissaoja/emissaoja
blackbricksoftware/gravityforms-civicrm-form-processor
al-rahabi/fancy-crud
jinas/dhivatar-component
mltstephane/laravel-quality
bernskiold/laravel-talentlms
bernskioldmedia/laravel-form-architect
youbar/easy-crud
c-point-labs/laravel-hostyourai
dbravoan/dbcms-admin-theme-dbcms
dbravoan/dbcms-theme-db-cyber
dbravoan/dbcms-blocks-basic
dbravoan/dbcms-module-content-audit
dbravoan/dbcms-module-ai-assistant
paygate-to/paygate-php
padosoft/askmydocs-connector-base
defstudio/supercharged-carbon
bernskioldmedia/laravel-matomo