PHP Classes
Icontem

File: thread_class.php


  Search   All class groups All class groups   Latest entries Latest entries   Top 10 charts Top 10 charts   Newsletter Newsletter   Blog Blog   Forums Forums   Help FAQ Help FAQ  
  Login   Register  
Recommend this page to a friend! ReTweet ReTweet Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Nicolas Iglesias  >  Threader  >  thread_class.php  
File: thread_class.php
Role: Class source
Content type: text/plain
Description: Main class
Class: Threader
Run multiple PHP scripts in parallel
 

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(=> array("pipe""r"), => array("pipe""w"),
            
=> array("pipe""w"));
        
$pipes = array();
        if (!empty(
$cmd)):
        
$this->threadName $name;
        
try
        
{
            
$this->rid proc_open("$cmd $vars"$descriptorspec$this->
                
pipesnull$_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;
    }
}
?>

 
  Advertise on this site Advertise on this site   Site map Site map   Statistics Statistics   Site tips Site tips   Privacy policy Privacy policy   Contact Contact  

For more information send a message to :
info at phpclasses dot org.
Copyright (c) Icontem 1999-2009 PHP Classes - PHP Class Scripts
  PHP Book Reviews - Reviews of books and other products