Multi-threading in PHP <ul><li>Dimitriy Remerov </li></ul>
Scopes <ul><li>CURL bots </li></ul><ul><li>Creating files such as PDF, DOC, etc. </li></ul><ul><li>Cycles or calculation t...
CURL bots <ul><li>If you need parallel CURL downloads, you actually don't need multi-processing, because you can use  curl...
Ways of using with web server <ul><li>Within web server environment you can run processes in background via  exec() this w...
PCNTL extension <ul><li>PHP have the Process Control extension. </li></ul><ul><li>You can use it if you built your PHP wit...
ZendX_Console_Process_Unix <ul><li>There is a wrapper for PCNTL that allows developers to spawn an object as a new process...
Simple Usage <ul><li>class  MyProcess  extends  ZendX_Console_Process_Unix </li></ul><ul><li>{ </li></ul><ul><li>protected...
Simple Usage <ul><li>$process1  =  new  MyProcess () ; </li></ul><ul><li>$process1 -> start () ; </li></ul><ul><li>$proces...
Other methods <ul><li>setVariable()  and  getVariable()  can be used from both the parent- and the child process to share ...
Other languages <ul><li>Don't forget that a web application doesn't have to be written completely in one language. </li></...
Upcoming SlideShare
Loading in...5
×

Multithreading in PHP

6,624

Published on

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
6,624
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
62
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Multithreading in PHP

  1. 1. Multi-threading in PHP <ul><li>Dimitriy Remerov </li></ul>
  2. 2. Scopes <ul><li>CURL bots </li></ul><ul><li>Creating files such as PDF, DOC, etc. </li></ul><ul><li>Cycles or calculation tasks optimization </li></ul>
  3. 3. CURL bots <ul><li>If you need parallel CURL downloads, you actually don't need multi-processing, because you can use curl_multi_* functions. </li></ul>
  4. 4. Ways of using with web server <ul><li>Within web server environment you can run processes in background via exec() this way: </li></ul><ul><li>exec ( '/path/cmd >/dev/null 2>&1 &' ) ; </li></ul><ul><li>Don't forget to escapeshellarg() your arguments if you are going to pass any. </li></ul>
  5. 5. PCNTL extension <ul><li>PHP have the Process Control extension. </li></ul><ul><li>You can use it if you built your PHP with </li></ul><ul><li>--enable-pcntl option. </li></ul><ul><li>PCNTL doesn't work on Windows and should not be used within a web server environment. </li></ul>
  6. 6. ZendX_Console_Process_Unix <ul><li>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. </li></ul><ul><li>It works only on *nix based systems only with CGI or CLI environment. Additionally, the shmop_* , pcntl_* and posix_* modules are required. </li></ul>
  7. 7. Simple Usage <ul><li>class MyProcess extends ZendX_Console_Process_Unix </li></ul><ul><li>{ </li></ul><ul><li>protected function _run () </li></ul><ul><li>{ </li></ul><ul><li>for ( $i = 0 ; $i < 10 ; $i ++ ) { </li></ul><ul><li>// Doing something really important which can't wait: </li></ul><ul><li>sleep ( 1 ) ; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  8. 8. Simple Usage <ul><li>$process1 = new MyProcess () ; </li></ul><ul><li>$process1 -> start () ; </li></ul><ul><li>$process2 = new MyProcess () ; </li></ul><ul><li>$process2 -> start () ; </li></ul><ul><li>while ( $process1 -> isRunning () || $process2 -> isRunning ()) { </li></ul><ul><ul><li>sleep ( 1 ) ; </li></ul></ul><ul><li>} </li></ul><ul><li>echo 'All processes completed' ; </li></ul>
  9. 9. Other methods <ul><li>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() . </li></ul>
  10. 10. Other languages <ul><li>Don't forget that a web application doesn't have to be written completely in one language. </li></ul><ul><li>There is an approach that called 'problem picks the language'. </li></ul><ul><li>Since PHP is not initially built for such things, you can use other languages for your multi-processing tasks. </li></ul>
  1. Gostou de algum slide específico?

    Recortar slides é uma maneira fácil de colecionar informações para acessar mais tarde.

×