This article is a simple example of the implementation of the PHP multitasking program, the specific code is as follows:
<?php error_reporting (E_all);
Set_time_limit (0); /** * PHP Multitasking implementation * With the help of proc_open * In fact, the call process (processes) * Can start multiple processes, you can use your imagination to do what you want to do, and then write an available * If you are running PHP on Linux and you enable the Pcntl module,
Use the PCNTL function to better * * */class Thread {protected $_pref;//process reference protected static $_instance = NULL;
protected $_pipes;
Private Function __construct () {$this->_pref = 0;
The public static function getinstance ($file) {if (null = self::$_instance) {self::$_instance = new self; $descriptor = Array (0 => array ("Pipe", "R"), 1 => Array ("Pipe", "w"), 2 => array ("Fil
E ","./error-output.txt "," a ");
Self::$_instance->_pref = Proc_open ("Php-q $file", $descriptor, Self::$_instance->_pipes);
return true;
The Public Function __destruct () {proc_close ($this->_pref);
$this->_pref = null;
}//Test code $file = __file__;
if (Empty ($argv [1])) {$t 2 = thread::getinstance ("$file 1"); $t 3 = thread::getinstance ("$file2 ");
$t 4 = thread::getinstance ("$file 3");
$t 5 = thread::getinstance ("$file 4");
$t 5 = thread::getinstance ("$file 5");
$t 5 = thread::getinstance ("$file 6");
$t 2 = thread::getinstance ("$file 7");
$t 3 = thread::getinstance ("$file 8");
$t 4 = thread::getinstance ("$file 9");
$t 5 = thread::getinstance ("$file 10");
$t 5 = thread::getinstance ("$file 11");
$t 5 = thread::getinstance ("$file 12");
echo "Main thread done\n";
else {$somecontent = "\r\n//~~~~~~~~~~~~-This request serial number is:". $argv [1];
Sleep (Mt_rand (0, 3));
$handle = fopen ($file, ' A + ');
Fwrite ($handle, $somecontent);
}