public
Last active

PHP Process Pool With Executor

  • Download Gist
Thread.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
<?php
//Found on http://www.alternateinterior.com/2007/05/multi-threading-strategies-in-php.html
//Modified by http://www.php-code.net
//Modified: add executor
class Thread {
var $pref ; // process reference
var $pipes; // stdio
var $buffer; // output buffer
var $output;
var $error;
var $timeout;
var $start_time;
 
function Thread($timeout) {
$this->pref = 0;
$this->buffer = "";
$this->pipes = (array)NULL;
$this->output = "";
$this->error="";
 
$this->start_time = time();
$this->timeout = $timeout;
}
 
static function Create ($command, $timeout) {
$t = new Thread($timeout);
$descriptor = array (0 => array ("pipe", "r"), 1 => array ("pipe", "w"), 2 => array ("pipe", "w"));
//Open the resource to execute $command
$t->pref = proc_open($command,$descriptor,$t->pipes);
//Set STDOUT and STDERR to non-blocking
stream_set_blocking ($t->pipes[1], 0);
stream_set_blocking ($t->pipes[2], 0);
return $t;
}
 
//See if the command is still active
function isActive () {
$this->buffer .= $this->listen();
$f = stream_get_meta_data ($this->pipes[1]);
return !$f["eof"];
}
 
//Close the process
function close () {
$r = proc_close ($this->pref);
$this->pref = NULL;
return $r;
}
 
//Send a message to the command running
function tell ($thought) {
fwrite ($this->pipes[0], $thought);
}
 
//Get the command output produced so far
function listen () {
$buffer = $this->buffer;
$this->buffer = "";
while ($r = stream_get_contents ($this->pipes[1])) {
$buffer .= $r;
}
return $buffer;
}
//Get the status of the current runing process
function getStatus(){
return proc_get_status($this->pref);
}
//See if the command is taking too long to run (more than $this->timeout seconds)
function isBusy(){
return ($this->start_time>0) && ($this->start_time+$this->timeout<time());
}
//What command wrote to STDERR
function getError () {
$buffer = "";
while ($r = fgets ($this->pipes[2], 1024)) {
$buffer .= $r;
}
return $buffer;
}
function getDurationSeconds() {
return time() - $this->start_time;
}
}
 
class Future {
var $taskId;
var $command;
var $result;
var $error;
var $finished = false;
var $started = false;
var $thread;
var $callback;
var $executor;
 
function Future($taskId, $command, $callback, $executor) {
$this->taskId = $taskId;
$this->command = $command;
$this->callback = $callback;
$this->executor = $executor;
}
function startup($timeout) {
$this->started = true;
$this->thread = Thread::create($this->command, $timeout);
}
function end($result, $error) {
$this->result = $result;
$this->error = $error;
$this->finished = true;
call_user_func($this->callback, $this->result, $this->error);
}
}
 
//Wrapper for Thread class
class ThreadPool{
var $poolSize;
var $defaultTimeout;
var $futures = array();
var $pipes = array();
var $queue = array();
var $output;
var $error;
var $index = 0;
function ThreadPool($size, $timeout) {
$this->poolSize = $size;
$this->defaultTimeout = $timeout;
}
function scheduleCommand($executor, $command, $callback) {
$future = new Future(""+$this->index++, $command, $callback, $executor);
$this->futures[$future->taskId] = $future;
$this->output[$future->taskId] = "";
$this->error[$future->taskId] = "";
if (count($this->pipes) >= $this->poolSize)
array_push($this->queue, $future);
else
$this->scheduleNow($future);
return $future;
}
function scheduleNow($future) {
$future->startup($this->defaultTimeout);
$this->pipes[$future->taskId] = $future->thread->pipes[1];
echo 'thread '.$future->taskId." started, command:".$future->command."\n";
}
function loop(){
while (count($this->pipes)>0){
$this->runOnce();
while(count($this->pipes)<$this->poolSize && count($this->queue)>0) {
$future = array_shift($this->queue);
$this->scheduleNow($future);
}
}
}
function runOnce() {
$streams = $this->pipes;
if (count($streams)>0){
$read = $streams;
$write = null;
$except = null;
stream_select($read, $write, $except, 1);
foreach ($read as $r) {
$id = array_search($r, $streams);
$thread = $this->futures[$id]->thread;
if ($thread->isActive()) {
$this->output[$id] .= $thread->listen();
if ($thread->isBusy()) {
$thread->close();
unset($this->pipes[$id]);
$this->futures[$id]->end($this->output[$id], "");
unset($this->output[$id]);
echo "thread $id timeout, duration ".$this->futures[$id]->thread->getDurationSeconds()."s, command:".$this->futures[$id]->command."\n";
}
} else {
$this->output[$id] .= $thread->listen();
$this->error[$id] .= $thread->getError();
$thread->close();
unset($this->pipes[$id]);
$this->futures[$id]->end($this->output[$id], $this->error[$id]);
unset($this->output[$id]);
unset($this->error[$id]);
echo "thread $id completed, duration ".$this->futures[$id]->thread->getDurationSeconds()."s, command:".$this->futures[$id]->command."\n";
}
}
}
}
}
 
class TaskExecutor {
var $threadPool;
var $callback;
function __construct($callbackFunc, $maxThreads=1, $taskTimeout=120) {
if ($taskTimeout <= 0)
$taskTimeout = 2147483647; //~never timeout
if ($maxThreads <= 0)
$maxThreads = 1;
if ($maxThreads >= 250)
$maxThreads = 250;
$this->threadPool = new ThreadPool($maxThreads, $taskTimeout);
$this->callback = $callbackFunc;
}
public function executeAsync($task) {
$this->threadPool->scheduleCommand($this, $task, $this->callback);
}
public function executeWaitTerminal($task) {
if (count($this->threadPool->pipes) > 0)
$this->waitForAllTerminal(); //execute after current tasks end
$future = $this->threadPool->scheduleCommand($this, $task, $this->callback);
while (!$future->finished) {
$this->threadPool->runOnce();
}
}
public function waitForAllTerminal() {
$this->threadPool->loop();
}
}
?>

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.