Location: PHPKode > scripts > Threading > threading/application.class.php
<?
//============================================================================
// Name        : application.class.php
// Author      : Alex Snet ( hide@address.com )
// Version     : 1.0
// Copyright   : free for use
// Description : threading (forking) on pure php
// Site         : http://alexsnet.ru (rus) and http://alexsnet.ru/en (eng)
//============================================================================
class Application
{
	public $child = false;
	private $pid = false;
	private $file;
	private $tmpdir;
	private $children = array();
	private $vars = array();
	
	public function __construct($argv,$argc)
	{
		$GET = array();
		$this->pid = getmypid();
		$this->file = str_replace(' ','\ ',getcwd()).'/'.$argv[0];
		for($i=1;$i<$argc;$i++)
		{
			if(substr($argv[$i],0,1)=='-' and ( isset($argv[($i+1)]) and substr($argv[($i+1)],0,1)!=='-'))
			{
				$GET[substr($argv[$i],1)] = $argv[($i+1)];
				$i++;
			}
			else
			{
				$GET[substr($argv[$i],1)] = true;
			}
		}
		$this->vars = $GET;
		if(isset($this->vars['tmpdir']))
		{
			$this->child = $this->vars['pid'];
			$this->tmpdir = $this->vars['tmpdir'];
			$this->ChildPIDs[$GET['pid']] = $this->pid;
		}
		else
		{
			$this->tmpdir = '/tmp/'.md5($this->file . time());
			mkdir($this->tmpdir);
		}
		mkdir($this->tmpdir.'/vars');
	}
	public function __destruct()
	{
		if($this->child == false)
		{
			if(file_exists($this->tmpdir.'/vars/'))
			{
				$vars = scandir($this->tmpdir.'/vars/');
				foreach($vars as $var) if(strlen($var)>3)unlink($this->tmpdir.'/vars/'.$var);
				rmdir($this->tmpdir.'/vars/');
			}
			if(file_exists($this->tmpdir.'/threads/')) rmdir($this->tmpdir.'/threads/');
			rmdir($this->tmpdir);
		}
		else
		{
			//unlink($this->tmpdir.'/threads/'.$this->child.'.pid');
		}
	}
	public function __get($var)
	{
		$val = false;
		if(file_exists($this->tmpdir.'/vars/'.md5($var)))
		{
		$val = file_get_contents($this->tmpdir.'/vars/'.md5($var));
		$val = unserialize($val);
		}
		return $val;
	}
	public function __set($var,$val)
	{
		$vall = $this->{$var};
		if($vall!=$val)
		{
			$fp = fopen($this->tmpdir.'/vars/'.md5($var), "w");
			if (flock($fp, LOCK_EX))
			{
			    fwrite($fp, serialize($val));
			    flock($fp, LOCK_UN);
			}else $this->{$var} = $val;
			fclose($fp);
		}
		echo $var. ' : ' . serialize($val) . chr(10);
	}
	public function startThreads($count=0)
	{
		mkdir($this->tmpdir.'/threads');
		for($i=1;$i<=$count;$i++)
		{
			$pid = $this->tmpdir.'/threads/'.$i.'.pid';
			$this->children[$i]['pid'] = popen("php {$this->file} -tmpdir {$this->tmpdir} -pid {$i} > {$pid}&",'r');
		}
	}
	public function childs()
	{
		$dd = 0;
		$darr = scandir($this->tmpdir.'/threads/');
		foreach($darr as $d) if($d!='.' and $d!='..') $dd++;
		return $dd;
	}
	public function write($args,$pid=false)
	{
		if(!is_array($args)) $args = array($args);
		$str = serialize($args);
		$f = fopen($this->tmpdir.'/'.$this->child.'.pid','w');
		fwrite($f,$str);
		fclose($f);
	}
}
$Application = new Application($argv,$argc);
?>
Return current item: Threading