Code example for implementing multi-process and multi-task in php
-
/**
- * PHP5-based implementation
- * Proc_open
- * You can start multiple processes. you can use your imagination to do what you want.
- * If you run php on linux and enable the pcntl module, it is better to use the pcntl function.
- * Last Modified: by bbs.it-home.org
- **/
- Error_reporting (E_ALL );
- Set_time_limit (0 );
-
Class Thread {
- Protected $ _ pref; // process reference
- Protected static $ _ instance = null;
- Protected $ _ pipes;
-
Private function _ construct (){
- $ This-> _ pref = 0;
- }
-
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 ("file", "./error-output.txt", ""),
- );
- Self: $ _ instance-> _ pref = proc_open ("php-q $ file", $ descriptor, self: $ _ instance-> _ pipes );
- Return true;
- }
-
Public function _ destruct (){
- Proc_close ($ this-> _ pref );
- $ This-> _ pref = null;
- }
- ?>
|