note 97329 added to function.proc-open
| From: | MatouHavlena-matous at havlena dot net | Date: | Wed, 14 Apr 2010 20:03:40 +0000 |
| Subject: | note 97329 added to function.proc-open | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-168244@lists.php.net to get a copy of this message | ||
There is some smart object Processes Manager which i have created for my application. It can control
the maximum of simultaneously running processes.
Proccesmanager class:
<?php
class Processmanager {
public $executable = "C:\\www\\_PHP5_2_10\\php";
public $root = "C:\\www\\parallelprocesses\\";
public $scripts = array();
public $processesRunning = 0;
public $processes = 3;
public $running = array();
public $sleep_time = 2;
function addScript($script, $max_execution_time = 300) {
$this->scripts[] = array("script_name" => $script,
"max_execution_time" => $max_execution_time);
}
function exec() {
$i = 0;
for(;;) {
// Fill up the slots
while (($this->processesRunning<$this->processes) and
($i<count($this->scripts))) {
echo "<span style='color: orange;'>Adding script:
".$this->scripts[$i]["script_name"]."</span><br />";
ob_flush();
flush();
$this->running[] =& new Process($this->executable, $this->root,
$this->scripts[$i]["script_name"],
$this->scripts[$i]["max_execution_time"]);
$this->processesRunning++;
$i++;
}
// Check if done
if (($this->processesRunning==0) and ($i>=count($this->scripts))) {
break;
}
// sleep, this duration depends on your script execution time, the longer execution time, the
longer sleep time
sleep($this->sleep_time);
// check what is done
foreach ($this->running as $key => $val) {
if (!$val->isRunning() or $val->isOverExecuted()) {
if (!$val->isRunning()) echo "<span style='color: green;'>Done:
".$val->script."</span><br />";
else echo "<span style='color: red;'>Killed:
".$val->script."</span><br />";
proc_close($val->resource);
unset($this->running[$key]);
$this->processesRunning--;
ob_flush();
flush();
}
}
}
}
}
?>
Process class:
<?php
class Process {
public $resource;
public $pipes;
public $script;
public $max_execution_time;
public $start_time;
function __construct(&$executable, &$root, $script, $max_execution_time) {
$this->script = $script;
$this->max_execution_time = $max_execution_time;
$descriptorspec = array(
0 => array('pipe', 'r'),
1 => array('pipe', 'w'),
2 => array('pipe', 'w')
);
$this->resource = proc_open($executable." ".$root.$this->script, $descriptorspec,
$this->pipes, null, $_ENV);
$this->start_time = mktime();
}
// is still running?
function isRunning() {
$status = proc_get_status($this->resource);
return $status["running"];
}
// long execution time, proccess is going to be killer
function isOverExecuted() {
if ($this->start_time+$this->max_execution_time<mktime()) return true;
else return false;
}
}
?>
Example of using:
<?php
$manager = new Processmanager();
$manager->executable = "C:\\www\\_PHP5_2_10\\php";
$manager->path = "C:\\www\\parallelprocesses\\";
$manager->processes = 3;
$manager->sleep_time = 2;
$manager->addScript("script1.php", 10);
$manager->addScript("script2.php");
$manager->addScript("script3.php");
$manager->addScript("script4.php");
$manager->addScript("script5.php");
$manager->addScript("script6.php");
$manager->exec();
?>
And possible output:
Adding script: script1.php
Adding script: script2.php
Adding script: script3.php
Done: script2.php
Adding script: script4.php
Killed: script1.php
Done: script3.php
Done: script4.php
Adding script: script5.php
Adding script: script6.php
Done: script5.php
Done: script6.php
----
Server IP: 69.147.83.197
Probable Submitter: 109.108.101.165
----
Manual Page -- http://www.php.net/manual/en/function.proc-open.php
Edit -- https://master.php.net/note/edit/97329
Del: integrated -- https://master.php.net/note/delete/97329/integrated
Del: useless -- https://master.php.net/note/delete/97329/useless
Del: bad code -- https://master.php.net/note/delete/97329/bad+code
Del: spam -- https://master.php.net/note/delete/97329/spam
Del: non-english -- https://master.php.net/note/delete/97329/non-english
Del: in docs -- https://master.php.net/note/delete/97329/in+docs
Del: other reasons-- https://master.php.net/note/delete/97329
Reject -- https://master.php.net/note/reject/97329
Search -- https://master.php.net/manage/user-notes.php