PHP Multi-Task Program instance parsing _php technique

Source: Internet
Author: User

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);
 }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.