joshdifabio/future-process

使用futures和promises进行PHP进程执行

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

This package is auto-updated.

Last update: 2024-08-26 04:21:44 UTC


README

Build Status Codecov Code Quality

简介

Future Process 是一个支持异步API和自动命令队列的面向对象的 proc_open

用法

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

非阻塞

我们可以使用 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();

阻塞

我们也可以同步地消费进程输出。

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

安装

使用 composer 安装 Future Process。

composer require joshdifabio/future-process

许可证

Future Process 在 MIT 许可下发布。