amphp/parallel
True parallel processing for PHP with AMPHP: run blocking tasks in worker processes or threads without blocking the event loop. Provides non-blocking concurrency tools and an easy worker pool API for distributing work; no extensions required (threads optional).
amphp/parallel aligns well with Laravel’s growing adoption of async/await (via Symfony’s Fiber or libraries like spatie/async) and Amp’s event loop. It enables true parallelism (process/thread-based) without blocking the main event loop, making it ideal for CPU-bound or I/O-bound workloads where synchronous execution would bottleneck performance.WorkerPool abstraction is a natural fit for Laravel’s queue systems (e.g., laravel-queue) or background job processing. It can replace or augment existing queue workers (e.g., laravel-horizon) for parallelizable tasks.amphp/parallel can integrate with async libraries (e.g., amphp/http-client) to offload blocking operations (e.g., HTTP requests, image processing) to worker pools without rewriting the entire app as async.ext-parallel for threads). Can coexist with Laravel’s service container via dependency injection.Illuminate\Queue\Worker) with WorkerPool for parallel job execution.Worker/WorkerPool in Artisan commands for parallel batch processing (e.g., CSV imports, reports).Channel for IPC between Laravel processes (e.g., real-time notifications via workers).AtomicCache) to avoid race conditions. Laravel’s service container (e.g., AppServiceProvider) won’t persist across workers.app() sparingly in tasks.boot() methods) may conflict with Amp’s event loop. Mixing synchronous Laravel code with async workers requires careful sequencing.Amp\run() to delegate to the event loop.monolog may not capture worker logs by default.psr/log) in tasks and use tools like Sentry for error tracking.ext-parallel) reduce this but require PHP 8.2+ ZTS.WorkerPool?supervisor, health checks).amphp/parallel. Laravel 9+ supports this.composer require amphp/amp.ext-parallel: For thread-based workers (PHP 8.2+ ZTS). Reduces process overhead but adds dependency.amphp/http-client: For non-blocking HTTP requests within workers (replaces Guzzle/Symfony HTTP Client in async contexts).amphp/cluster: For advanced IPC (e.g., socket sharing between workers).PDO, Eloquent) since they’re not serializable. Use connection pooling or lazy initialization.WorkerPool.// Before: Synchronous command
public function handle() {
$data = $this->fetchBlockingData(); // Blocks event loop
$this->processData($data);
}
// After: Async worker
public function handle() {
$workerPool = new WorkerPool(4);
$execution = $workerPool->submit(new FetchTask($url));
$data = $execution->await();
$this->processData($data);
}
WorkerPool for parallel job execution.Illuminate\Queue\Worker with a custom worker that uses WorkerPool for batch jobs.Route::get('/data', function () {
$workerPool = app(WorkerPool::class);
$execution = $workerPool->submit(new ApiFetchTask('https://api.example.com'));
$data = $execution->await();
return response()->json($data);
});
Amp\run().Amp\run(function () {
$loop = Amp\Loop::get();
$workerPool = new WorkerPool(4);
$execution = $workerPool->submit(new Task());
$result = $execution->await();
return response()->json($result);
});
app()->make()). Prefer explicit dependencies.Amp\delay() or Amp\Promise for async logic.DB::connection() or PDO with lazy loading.amphp/http-client in workers for non-blocking requests.ext-parallel: Optional for threads. Fallback to processes if unavailable.pcntl/posix: Required for process-based workers (usually available on Linux).WorkerPool in a service provider (e.g., AppServiceProvider).public function register() {
$this->app->singleton(WorkerPool::class, function () {
return new WorkerPool(4);
});
}
Task implementations for blocking operations.AtomicCache).Worker::submit() in try-catch blocks to handle task failures.try {
$execution = $workerPool->submit(new Task());
$result = $execution->await();
} catch (Throwable $e) {
Log
How can I help you explore Laravel packages today?