PHP Classes
elePHPant
Icontem

File: thread_class.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Nicolas Iglesias  >  Threader  >  thread_class.php  >  Download  
File: thread_class.php
Role: Class source
Content type: text/plain
Description: Main class
Class: Threader
Run multiple PHP scripts in parallel
Author: By
Last change: Changed comments.
Date: 6 years ago
Size: 1,983 bytes
 

Contents

Class file image Download
<?php
/**
 * Threader
 *
 * @package Threader Class
 * @author Nicolas Iglesias - nfiglesias@gmail.com - cleversight.com
 * @copyright 2008
 * @version 1.0
 * @access public
 */
class Threader
{
    var
$threadName = null;
    var
$rid = null;
    var
$error = null;
    var
$pipes = array();
    var
$active = false;

 
/**
   * Threader::Threader()
   *
   * The constructor which opens the process.
   *
   * @param mixed $cmd - Execute a shell command
   * @param mixed $vars - Pass arguments to shell command
   * @param string $name - Identifies your thread (useful for debug)
   * @return void
   */
   
function Threader($cmd = null, $vars = null, $name = null)
    {
       
$descriptorspec = array(0 => array("pipe", "r"), 1 => array("pipe", "w"),
           
2 => array("pipe", "w"));
       
$pipes = array();
        if (!empty(
$cmd)):
       
$this->threadName = $name;
        try
        {
           
$this->rid = proc_open("$cmd $vars", $descriptorspec, $this->
               
pipes, null, $_ENV);
           
$this->active = true;
        }
        catch (
exception $e)
        {
           
$this->active = false;
           
$this->error = $e->getMessage();
        }
        endif;
    }

 
/**
   * Threader::listen()
   *
   * While $this->active == true, you can monitor a thread by calling this method.
   *
   * @return string - Will return the output of the running process
   */
   
public function listen()
    {
        if (
is_resource($this->rid) && !empty($this->pipes))
        {
           
$stdout = (isset($this->pipes['1'])) ? $this->pipes['1'] : null;
            return
fgets($stdout);
        }
        else
        {
            return
null;
        }
    }

    function
__destruct()
    {
       
$this->active = false;
        if (
is_resource($this->rid))
       
proc_close($this->rid);
        if (!empty(
$this->error))
        echo
$this->error;
    }
}
?>