webmozarts/console-parallelization
Parallelize Symfony Console commands using multiple processes. A main process distributes items to child workers, restarts workers after segments to avoid slowdown, and supports batching with hooks for setup/teardown (e.g., DB flush) for faster bulk jobs.
symfony/console). Laravel’s Artisan commands can extend ParallelCommand or use the Parallelization trait with minimal changes.Artisan already includes this, so integration is plugin-like.| Risk Area | Assessment | Mitigation Strategy |
|---|---|---|
| Process Management | Laravel’s Artisan runs in a single process by default. Spawning child processes may require custom CLI handling (e.g., pcntl extensions, Docker constraints). |
Test in a Dockerized Laravel environment first. Use --processes=N to limit resource usage. Monitor memory leaks with memory_get_usage(). |
| Stateful Services | Laravel’s service container is not shared between processes. Child processes will lose access to cached services (e.g., Doctrine DBAL, Redis). | Use subscribed services or dependency injection (fetch fresh instances in child processes). Avoid global singletons. |
| Error Handling | Child process failures silently kill the process. Main process must handle partial failures gracefully (e.g., retries, logging). | Implement createErrorHandler() to log failures centrally. Use --main-process for debugging. |
| I/O Bottlenecks | STDIN/STDOUT communication between processes can become a blocking factor for high-throughput commands. | Benchmark with small vs. large items. Optimize segmentSize/batchSize to balance CPU and I/O. Consider memory-mapped files for zero-copy data transfer if items are large. |
| Laravel-Specific Quirks | Laravel’s bootstrapping (e.g., bootstrap/app.php) may not play well with child process isolation. |
Extend ParallelCommand to re-initialize Laravel’s kernel in child processes if needed (high effort; prefer stateless operations). |
| Testing Complexity | Parallel commands are hard to unit test due to process isolation. | Mock ParallelExecutorFactory for unit tests. Use --main-process for integration tests. |
migrate, db:seed, custom bulk processors).--processes=N) based on server CPU cores?memory_get_peak_usage().)OutputInterface or a central logger.)parallel:workers) achieve similar goals with less risk?| Laravel Component | Compatibility | Notes |
|---|---|---|
| Console/Artisan | ✅ Full Support | Extend ParallelCommand or use Parallelization trait. Replace Artisan::call() with parallelized commands where needed. |
| Service Container | ⚠️ Partial Support | Child processes cannot share Laravel’s container. Use subscribed services or constructor injection to avoid stale dependencies. |
| Database (Eloquent) | ⚠️ Requires Care | Each child process needs a fresh DB connection. Avoid global singletons (e.g., DB::connection()). |
| Queues | ❌ Not Directly Replaced | Parallelization is for synchronous batch processing; queues are for asynchronous tasks. Use both for hybrid workflows. |
| Logging | ✅ Works with Monolog | Child process logs can be merged in the main process via OutputInterface. |
| Testing (PHPUnit) | ⚠️ Complex | Use --main-process for tests. Mock ParallelExecutorFactory for unit tests. |
ImportUsersCommand) to use ParallelCommand.--processes=2 and compare performance vs. serial execution.--main-process for debugging.Cache::store()) with per-process instances.createContainer() to customize the child process container if needed.| Scenario | Compatibility | Workaround |
|---|---|---|
| Laravel 10+ (PHP 8.1+) | ✅ Full Support | No changes needed. |
| Windows Servers | ❌ Unsupported (requires pcntl extension) |
Use Docker/Linux containers or avoid parallelization on Windows. |
| Custom Service Providers | ⚠️ May Break if services are stateful | Use subscribed services or rebuild the container in child processes. |
| Doctrine DBAL/Eloquent | ⚠️ Works if connections are managed per process | Avoid DB::connection() singletons; use createConnection() in child processes. |
| Symfony Process Component | ✅ Compatible (under the hood) | No conflicts expected. |
app:generate-reports).migrate, db:seed).--no-parallel is passed).How can I help you explore Laravel packages today?