joshdifabio/future-process

Process execution for PHP using futures and promises

v0.2.0 2015-04-06 15:48 UTC

This package is auto-updated.

Last update: 2024-10-26 04:36:35 UTC


README

Build Status Codecov Code Quality

Introduction

Future Process is object-oriented proc_open with an asynchronous API and automatic queueing of commands.

Usage

// 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");

Non-blocking

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();

Blocking

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";

Installation

Install Future Process using composer.

composer require joshdifabio/future-process

License

Future Process is released under the MIT license.