Download the PHP package spatie/async without Composer
On this page you can find all versions of the php package spatie/async. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
All versions of async with dependencies
Informations about the package async
Asynchronous and parallel PHP
This library provides a small and easy wrapper around PHP's PCNTL extension. It allows running of different processes in parallel, with an easy-to-use API.
We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.
You can install the package via composer:
When creating asynchronous processes, you'll get an instance of
You can add the following event hooks on a process.
Instead of using methods on the
$pool object, you may also use the
await helper functions.
Error is thrown from within a child process, it can be caught per process by specifying a callback in the
If there's no error handler added, the error will be thrown in the parent process when calling
If the child process would unexpectedly stop without throwing an
the output written to
stderr will be wrapped and thrown as
Spatie\Async\ParallelError in the parent process.
Catching exceptions by type
By type hinting the
catch functions, you can provide multiple error handlers,
each for individual types of errors.
Note that as soon as an exception is handled, it won't trigger any other handlers
Stopping a pool
If you need to stop a pool early, because the task it was performing has been completed by one
of the child processes, you can use the
$pool->stop() method. This will prevent the
pool from starting any additional processes.
Note that a pool will be rendered useless after being stopped, and a new pool should be created if needed.
Using another PHP binary
By default the pool will use
php to execute its child processes. You can configure another binary like so:
Working with tasks
Besides using closures, you can also work with a
Task is useful in situations where you need more setup work in the child process.
Because a child process is always bootstrapped from nothing, chances are you'll want to initialise eg. the dependency container before executing the task.
Task class makes this easier to do.
If you want to encapsulate the logic of your task, but don't want to create a full blown
you may also pass an invokable object to the
You're free to create as many pools as you want, each pool has its own queue of processes it will handle.
A pool is configurable by the developer:
If the required extensions (
posix) are not installed in your current PHP runtime, the
Pool will automatically fallback to synchronous execution of tasks.
Pool class has a static method
isSupported you can call to check whether your platform is able to run asynchronous processes.
If you're using a
Task to run processes, only the
run method of those tasks will be called when running in synchronous mode.
Behind the curtains
When using this package, you're probably wondering what's happening underneath the surface.
We're using the
symfony/process component to create and manage child processes in PHP.
By creating child processes on the fly, we're able to execute PHP scripts in parallel.
This parallelism can improve performance significantly when dealing with multiple synchronous tasks,
which don't really need to wait for each other.
By giving these tasks a separate process to run on, the underlying operating system can take care of running them in parallel.
There's a caveat when dynamically spawning processes: you need to make sure that there won't be too many processes at once,
or the application might crash.
Pool class provided by this package takes care of handling as many processes as you want
by scheduling and running them when it's possible.
That's the part that
$pool->add() does. Now let's look at what
When multiple processes are spawned, each can have a separate time to completion. One process might eg. have to wait for a HTTP call, while the other has to process large amounts of data. Sometimes you also have points in your code which have to wait until the result of a process is returned.
This is why we have to wait at a certain point in time: for all processes on a pool to finish, so we can be sure it's safe to continue without accidentally killing the child processes which aren't done yet.
Waiting for all processes is done by using a
while loop, which will wait until all processes are finished.
Determining when a process is finished is done by using a listener on the
This signal is emitted when a child process is finished by the OS kernel.
As of PHP 7.1, there's much better support for listening and handling signals,
making this approach more performant than eg. using process forks or sockets for communication.
You can read more about it here.
When a process is finished, its success event is triggered, which you can hook into with the
Likewise, when a process fails or times out, the loop will update that process' status and move on.
When all processes are finished, the while loop will see that there's nothing more to wait for, and stop.
This is the moment your parent process can continue to execute.
Comparison to other libraries
We've written a blog post containing more information about use cases for this package, as well as making comparisons to other asynchronous PHP libraries like ReactPHP and Amp: http://stitcher.io/blog/asynchronous-php.
Please see CHANGELOG for more information what has changed recently.
Please see CONTRIBUTING for details.
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
You're free to use this package, but if it makes it to your production environment we highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using.
Our address is: Spatie, Kruikstraat 22, 2018 Antwerp, Belgium.
We publish all received postcards on our company website.
- Brent Roose
- All Contributors
The MIT License (MIT). Please see License File for more information.