KEMBAR78
Multithreading in PHP | PPT
Multi-threading in PHP Dimitriy Remerov
Scopes CURL bots Creating files such as PDF, DOC, etc. Cycles or calculation tasks optimization
CURL bots If you need parallel CURL downloads, you actually don't need multi-processing, because you can use  curl_multi_*  functions.
Ways of using with web server Within web server environment you can run processes in background via  exec() this way: exec ( '/path/cmd >/dev/null 2>&1 &' ) ; Don't forget to  escapeshellarg()  your arguments if you are going to pass any.
PCNTL extension PHP have the Process Control extension. You can use it if you built your PHP with  --enable-pcntl  option. PCNTL doesn't work on Windows and should not be used within a web server environment.
ZendX_Console_Process_Unix There is a wrapper for PCNTL that allows developers to spawn an object as a new process, and so do multiple tasks in parallel on console environments. It works only on *nix based systems only with CGI or CLI environment. Additionally, the  shmop_* ,  pcntl_*  and  posix_*  modules are required.
Simple Usage class  MyProcess  extends  ZendX_Console_Process_Unix { protected  function  _run () { for   ( $i  =  0 ;  $i  <  10 ;  $i ++ )   { // Doing something really important which can't wait: sleep ( 1 ) ; } } }
Simple Usage $process1  =  new  MyProcess () ; $process1 -> start () ; $process2  =  new  MyProcess () ; $process2 -> start () ; while   ( $process1 -> isRunning ()  ||  $process2 -> isRunning ())   { sleep ( 1 ) ; } echo   'All processes completed' ;
Other methods setVariable()  and  getVariable()  can be used from both the parent- and the child process to share variables. To observe the alive status, the child process should call  _setAlive()  in a frequent interval, so that the parent process can check the last alive time via  getLastAlive() . To get the PID of the child process, the parent can call  getPid() .
Other languages Don't forget that a web application doesn't have to be written completely in one language. There is an approach that called 'problem picks the language'.  Since PHP is not initially built for such things, you can use other languages for your multi-processing tasks.

Multithreading in PHP

  • 1.
    Multi-threading in PHPDimitriy Remerov
  • 2.
    Scopes CURL botsCreating files such as PDF, DOC, etc. Cycles or calculation tasks optimization
  • 3.
    CURL bots Ifyou need parallel CURL downloads, you actually don't need multi-processing, because you can use curl_multi_* functions.
  • 4.
    Ways of usingwith web server Within web server environment you can run processes in background via exec() this way: exec ( '/path/cmd >/dev/null 2>&1 &' ) ; Don't forget to escapeshellarg() your arguments if you are going to pass any.
  • 5.
    PCNTL extension PHPhave the Process Control extension. You can use it if you built your PHP with --enable-pcntl option. PCNTL doesn't work on Windows and should not be used within a web server environment.
  • 6.
    ZendX_Console_Process_Unix There isa wrapper for PCNTL that allows developers to spawn an object as a new process, and so do multiple tasks in parallel on console environments. It works only on *nix based systems only with CGI or CLI environment. Additionally, the shmop_* , pcntl_* and posix_* modules are required.
  • 7.
    Simple Usage class MyProcess extends ZendX_Console_Process_Unix { protected function _run () { for ( $i = 0 ; $i < 10 ; $i ++ ) { // Doing something really important which can't wait: sleep ( 1 ) ; } } }
  • 8.
    Simple Usage $process1 = new MyProcess () ; $process1 -> start () ; $process2 = new MyProcess () ; $process2 -> start () ; while ( $process1 -> isRunning () || $process2 -> isRunning ()) { sleep ( 1 ) ; } echo 'All processes completed' ;
  • 9.
    Other methods setVariable() and getVariable() can be used from both the parent- and the child process to share variables. To observe the alive status, the child process should call _setAlive() in a frequent interval, so that the parent process can check the last alive time via getLastAlive() . To get the PID of the child process, the parent can call getPid() .
  • 10.
    Other languages Don'tforget that a web application doesn't have to be written completely in one language. There is an approach that called 'problem picks the language'. Since PHP is not initially built for such things, you can use other languages for your multi-processing tasks.