spatie/fork
Run PHP code concurrently using lightweight process forking. Define multiple closures and execute them in parallel, collecting results in order. Requires PHP 8 with pcntl (CLI only) and posix extensions on Unix-like systems.
spatie/fork package enables process-based concurrency in PHP, leveraging pcntl_fork() to run tasks in parallel. This is a strong fit for CPU-bound or I/O-bound workloads where traditional synchronous execution would bottleneck performance (e.g., batch processing, image/video manipulation, API scraping, or heavy computations).reactphp), this is not event-loop-based, making it incompatible with frameworks relying on async I/O (e.g., Swoole, RoadRunner). However, it integrates seamlessly with synchronous Laravel applications.pcntl_wait().pcntl, posix extensions required).| Risk Area | Severity | Mitigation Strategy |
|---|---|---|
| Process Management | High | Implement pcntl_signal_dispatch() for graceful shutdowns. Use pcntl_waitpid() to avoid zombie processes. |
| Memory Leaks | Medium | Isolate heavy tasks; avoid sharing large objects between parent/child. |
| Cross-Platform | Medium | Requires pcntl (Linux/macOS only). Windows users need WSL or alternative (e.g., parallel CLI). |
| State Consistency | High | Use database transactions or distributed locks for shared resources. Avoid in-memory shared state. |
| Debugging | High | Logs from child processes may be hard to correlate. Implement structured logging (e.g., monolog with process IDs). |
spatie/fork better than Laravel Queues + Redis, or async PHP (e.g., Swoole)?ulimit restrictions)?shouldQueue() could help.)parallel CLI or Laravel’s built-in process helper suffice for simpler cases?spatie/fork overkill for lightweight parallelism (e.g., <10 tasks)?php artisan optimize:forked).Illuminate\Bus\Queueable and fork in handle()).spatie/fork + Laravel Queues for distributed workloads).php artisan task:heavy) that forks 2–4 processes.pcntl_signal() to handle SIGTERM for graceful shutdowns.Illuminate\Bus\Queueable and fork in handle():
public function handle()
{
$fork = new Fork();
$fork->run(function () {
// Heavy task in child process
});
}
dispatch(new HeavyJob()).$fork->run(function () {
dispatch(new RetryableJob())->onQueue('high-priority');
});
DB::table('forked_tasks')->insert([
'process_id' => getmypid(),
'status' => 'running',
'job_id' => $job->id,
]);
pcntl, posix (Linux/macOS only). Test with:
php -m | grep -E 'pcntl|posix'
sem_acquire).pcntl_waitpid() or signals to avoid zombies.htop/ps to monitor forked processes.pcntl_alarm() for timeouts).pcntl_wait() in parent.sys_getloadavg().SIGCHLD is ignored or handled.spatie/fork).memory_get_usage().| Failure Scenario | Impact | Mitigation |
|---|---|---|
| Parent process dies | Orphaned child processes. | Use pcntl_signal(SIGCHLD, ...) to reap zombies. |
| Child process hangs | Blocked I/O or infinite loops. | Implement pcntl_alarm() timeouts. |
| Database deadlocks | Shared state races. | Use transactions or locks. |
| Resource starvation | Too many forks OOM. | Limit concurrency (e.g., semaphores). |
| Signal interference | SIGKILL terminates all. |
How can I help you explore Laravel packages today?