- How do I install spatie/async in a Laravel project?
- Run `composer require spatie/async` in your project directory. No additional configuration is needed unless you’re using Windows, where you’ll need WSL or a custom setup for PCNTL support.
- Can I use spatie/async for Laravel queue jobs?
- Yes, but it’s not a direct replacement. Use it for CPU-bound tasks within a job’s handle method, then dispatch the job to Laravel’s queue system. Async runs in parallel *within* the job, not across workers.
- What Laravel versions does spatie/async support?
- The package is framework-agnostic but tested with Laravel 8.x, 9.x, and 10.x. It works with any PHP 8.0+ app, including vanilla PHP or Symfony projects.
- How do I handle errors in parallel processes?
- Use the `catch()` method on each task to log exceptions. For critical tasks, wrap the `Pool::wait()` call in a try-catch and log errors to a service like Sentry or Laravel’s logging system.
- Is spatie/async suitable for Windows-hosted Laravel apps?
- No, PCNTL is not natively supported on Windows. Use WSL (Windows Subsystem for Linux) or a Linux-based hosting environment like Forge, Heroku, or shared hosting with PCNTL enabled.
- Can I dynamically add tasks to a Pool while it’s running?
- Yes, tasks can be added to the Pool at any time before calling `wait()`. However, all tasks must complete before the Pool resolves, so avoid adding tasks after `wait()` is called.
- How does spatie/async compare to Laravel’s queue system?
- Async runs tasks in parallel *within a single request* (true parallelism via PCNTL), while Laravel queues distribute jobs across workers (concurrency). Use Async for CPU-heavy batch jobs and queues for background tasks.
- What’s the best way to monitor parallel processes?
- Use `pcntl_signal_dispatch()` for custom signal handling or tools like `htop`/`strace` to monitor child processes. For Laravel, log process IDs and execution times via the `then()`/`catch()` callbacks.
- Does spatie/async support retries for failed tasks?
- No, it lacks built-in retry logic. For resilience, combine it with Laravel’s queue system or manually wrap tasks in retry loops using `try-catch` and a counter.
- Can I use spatie/async for API rate-limited tasks (e.g., parallel API calls)?
- Yes, but throttle the Pool size (e.g., `Pool::create()->pool(5)`) to avoid hitting rate limits. Use `then()` to handle responses and `catch()` to log throttling errors.