amphp/parallel
True parallel processing for PHP with AMPHP: run blocking work in worker processes or threads without blocking the event loop and without extensions. Includes non-blocking concurrency tools plus an opinionated worker pool API for submitting tasks and awaiting results.
amphp/parallel aligns with Laravel’s growing adoption of async/await (via Symfony’s Fiber and Amp) and event-driven architectures (e.g., queues, jobs). It bridges blocking I/O (e.g., file_get_contents, CPU-heavy tasks) with non-blocking event loops, reducing latency in high-concurrency scenarios.WorkerPool interface abstracts process/thread management, making it ideal for Laravel’s job queues (e.g., Illuminate\Bus\Queue) or batch processing (e.g., Illuminate\Support\Facades\Bus::batch). Tasks can be distributed across workers without manual process orchestration.Task interface), which may conflict with Laravel’s dependency-injected services (e.g., repositories, Eloquent models). Workarounds include:
LocalCache/AtomicCache for shared state (as shown in the docs), but avoid Laravel’s service container (which isn’t serializable).sync:work) can leverage amphp/parallel for parallel task execution within a single request, while async queues (e.g., database, redis) remain unchanged.illuminate/support includes Amp\Loop (since v9.x), enabling seamless event loop integration. However, Laravel’s default Swoole/ReactPHP servers may require adjustments to delegate to Amp’s loop.php artisan queue:work) with a custom worker using WorkerPool to process jobs in parallel. Example:
$pool = new WorkerPool(new ProcessContextFactory(), 4); // 4 workers
$pool->submit(new DatabaseJobTask($job));
Worker::submit() in middleware or controllers for blocking operations (e.g., PDF generation, API calls to external services) without blocking the HTTP request.pdo_pgsql/pdo_mysql with persistent mode) or lazy-load connections in Task::run().Task interfaces, Channel IPC, and cancellation patterns.Task::run()) may require custom error handling or logging (e.g., WorkerPool::onError()).ext-parallel) offer lower latency but require PHP 8.2+ ZTS. Processes are more portable but slower to spawn.Event::dispatch()) won’t propagate to workers. Use Channel IPC or a shared message bus (e.g., Redis) for cross-worker communication.Channel and Cancellation. Integration tests may need custom WorkerPool setups.AtomicCache vs. LocalCache?ext-parallel) or must we rely on processes? Threads reduce latency but require PHP 8.2+ ZTS.WorkerPool error handlers?amphp/parallel for specific jobs (e.g., high-priority batch jobs)?Amp server (e.g., Amp\Http\Server)?WorkerPool extensions.ReactPHP/Swoole loop with Amp\Loop in custom server setups (e.g., CLI commands, queues). Use Amp\Loop::run() in artisan commands or queue workers.Illuminate\Contracts\Queue\Job to support Task interface. Example:
class ParallelJob implements Task, ShouldQueue {
public function run(Channel $channel, Cancellation $cancellation) { ... }
public function handle() { ... } // Legacy Laravel queue method
}
Amp\Http\Server for async routes (e.g., WebSockets, long-polling) alongside Laravel’s router. For sync routes, offload blocking tasks to workers via middleware.reactphp/react (Laravel’s default). Resolve via composer overrides or custom bootstrap.file_get_contents in a job) with Worker::submit().Task class for the blocking operation.Amp\Loop::run() in a custom artisan command for testing.WorkerPool.Illuminate\Queue\Worker to use WorkerPool for job execution.4 * CPU cores).logs table).$pool = new WorkerPool(new ProcessContextFactory(), config('queue.workers'));
$pool->submit(new DatabaseJobTask($job));
WorkerPool.Future\await() to block only the worker, not the HTTP request.public function handle($request, Closure $next) {
$task = new ImageProcessingTask($request->file('image'));
$future = Worker\submit($task);
$result = $future->await(); // Blocks only the worker
return $next($request->merge(['processed_image' => $result]));
}
Amp\Http\Server for async routes.WorkerPool.reactphp/event-loop with amphp/amp in config/app.php.Illuminate\Contracts\Container,How can I help you explore Laravel packages today?