Future Process is object-oriented proc_open
with an asynchronous API and automatic queueing of commands.
// we use Shell to start new processes
$shell = new \FutureProcess\Shell;
// run a maximum of 5 concurrent processes - additional ones will be queued
$shell->setProcessLimit(5);
// let's download this package's license file from GitHub using wget
$url = 'https://raw.githubusercontent.com/joshdifabio/future-process/master/LICENSE';
$process = $shell->startProcess("wget -O - $url");
We can consume the process output using promises.
// this will not block, even if the process is queued
$process->then(function ($process) {
echo "Downloading file...\n";
});
// this will not block, even if the process is queued
$process->getResult()->then(function ($result) {
echo "File contents:\n{$result->readFromPipe(1)}\n";
});
// this will block until all processes have exited
$shell->wait();
We can also consume the process output synchronously.
// this will block until the process starts
$process->wait();
echo "Downloading file...\n";
// this will block until the process exits
echo "File contents:\n{$process->getResult()->readFromPipe(1)}\n";
Install Future Process using composer.
composer require joshdifabio/future-process
Future Process is released under the MIT license.