SlideShare a Scribd company logo
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.

More Related Content

What's hot (20)

PDF
3 rd animation
divyalakshmi77
 
PPT
Advanced Scripting - 2 (Ch-8)
MohsinHusenManasiya
 
PDF
Proces
samof76
 
PDF
Devinsampa nginx-scripting
Tony Fabeen
 
PDF
Rcpp11
Romain Francois
 
PPTX
while and for Loops
Ray0uni
 
PDF
Protobuf & Code Generation + Go-Kit
Manfred Touron
 
ODP
Django debugging
Glen Zangirolami
 
PDF
Cocoapods sane library management for xcode
leonardehrenfried
 
DOCX
Note
Posoffaith1
 
PPTX
Job Automation using Linux
Jishnu Pradeep
 
ODP
Cron
Iryney Baran
 
PDF
CRONtab Tutorial
Joseph ...
 
PDF
DevOps in PHP environment
Evaldo Felipe
 
PDF
Git introduction
sppmg
 
PDF
A Partial Multiverse Model of Time Travel for Debugging
awwaiid
 
PDF
Unleash your build with nuke
Todor Todorov
 
PDF
Scaling FastAGI Applications with Go
Digium
 
PPTX
Asynchronous programming
Chester Hartin
 
PDF
Asynchronous JavaScript and Promises
Senthil Kumar
 
3 rd animation
divyalakshmi77
 
Advanced Scripting - 2 (Ch-8)
MohsinHusenManasiya
 
Proces
samof76
 
Devinsampa nginx-scripting
Tony Fabeen
 
while and for Loops
Ray0uni
 
Protobuf & Code Generation + Go-Kit
Manfred Touron
 
Django debugging
Glen Zangirolami
 
Cocoapods sane library management for xcode
leonardehrenfried
 
Job Automation using Linux
Jishnu Pradeep
 
CRONtab Tutorial
Joseph ...
 
DevOps in PHP environment
Evaldo Felipe
 
Git introduction
sppmg
 
A Partial Multiverse Model of Time Travel for Debugging
awwaiid
 
Unleash your build with nuke
Todor Todorov
 
Scaling FastAGI Applications with Go
Digium
 
Asynchronous programming
Chester Hartin
 
Asynchronous JavaScript and Promises
Senthil Kumar
 

Similar to Multithreading in PHP (20)

PPT
PHP CLI: A Cinderella Story
Mike Lively
 
PPTX
Cutting Back Processing Time
Henrique Moody
 
PDF
Meet a parallel, asynchronous PHP world
Steve Maraspin
 
PPT
Session Server - Maintaing State between several Servers
Stephan Schmidt
 
PDF
Portable PHP
weltling
 
ODP
The promise of asynchronous php
Wim Godden
 
ODP
The promise of asynchronous php
Wim Godden
 
PDF
A look at FastCgi & Mod_PHP architecture
Aimee Maree
 
PDF
From Generator to Fiber the Road to Coroutine in PHP
Albert Chen
 
ODP
The promise of asynchronous PHP
Wim Godden
 
PDF
Php through the eyes of a hoster confoo
Combell NV
 
PDF
Apache HTTPd Server 2.2 Presentation
ultimatetux
 
PDF
Running php on nginx
Harald Zeitlhofer
 
PDF
How PHP works
Atlogys Technical Consulting
 
PPTX
Frameworkless CLI app in PHP
Max Bodnar
 
PDF
Php through the eyes of a hoster phpbnl11
Combell NV
 
PDF
"Swoole: double troubles in c", Alexandr Vronskiy
Fwdays
 
PDF
Php through the eyes of a hoster
Combell NV
 
PDF
You Got Async in my PHP!
Chris Tankersley
 
PPTX
Interoperable PHP
weltling
 
PHP CLI: A Cinderella Story
Mike Lively
 
Cutting Back Processing Time
Henrique Moody
 
Meet a parallel, asynchronous PHP world
Steve Maraspin
 
Session Server - Maintaing State between several Servers
Stephan Schmidt
 
Portable PHP
weltling
 
The promise of asynchronous php
Wim Godden
 
The promise of asynchronous php
Wim Godden
 
A look at FastCgi & Mod_PHP architecture
Aimee Maree
 
From Generator to Fiber the Road to Coroutine in PHP
Albert Chen
 
The promise of asynchronous PHP
Wim Godden
 
Php through the eyes of a hoster confoo
Combell NV
 
Apache HTTPd Server 2.2 Presentation
ultimatetux
 
Running php on nginx
Harald Zeitlhofer
 
Frameworkless CLI app in PHP
Max Bodnar
 
Php through the eyes of a hoster phpbnl11
Combell NV
 
"Swoole: double troubles in c", Alexandr Vronskiy
Fwdays
 
Php through the eyes of a hoster
Combell NV
 
You Got Async in my PHP!
Chris Tankersley
 
Interoperable PHP
weltling
 
Ad

Recently uploaded (20)

PDF
LLMs.txt: Easily Control How AI Crawls Your Site
Keploy
 
PDF
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
PDF
CIFDAQ Market Insights for July 7th 2025
CIFDAQ
 
PPTX
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
PDF
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
PPTX
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
PDF
CIFDAQ Weekly Market Wrap for 11th July 2025
CIFDAQ
 
PDF
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
PDF
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
PPTX
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
PDF
What Makes Contify’s News API Stand Out: Key Features at a Glance
Contify
 
PDF
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
PPTX
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
PDF
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
PDF
Biography of Daniel Podor.pdf
Daniel Podor
 
PPTX
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
PDF
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
PDF
NewMind AI - Journal 100 Insights After The 100th Issue
NewMind AI
 
PDF
HCIP-Data Center Facility Deployment V2.0 Training Material (Without Remarks ...
mcastillo49
 
PDF
IoT-Powered Industrial Transformation – Smart Manufacturing to Connected Heal...
Rejig Digital
 
LLMs.txt: Easily Control How AI Crawls Your Site
Keploy
 
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
CIFDAQ Market Insights for July 7th 2025
CIFDAQ
 
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
CIFDAQ Weekly Market Wrap for 11th July 2025
CIFDAQ
 
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
What Makes Contify’s News API Stand Out: Key Features at a Glance
Contify
 
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
Biography of Daniel Podor.pdf
Daniel Podor
 
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
NewMind AI - Journal 100 Insights After The 100th Issue
NewMind AI
 
HCIP-Data Center Facility Deployment V2.0 Training Material (Without Remarks ...
mcastillo49
 
IoT-Powered Industrial Transformation – Smart Manufacturing to Connected Heal...
Rejig Digital
 
Ad

Multithreading in PHP

  • 1. Multi-threading in PHP Dimitriy Remerov
  • 2. Scopes CURL bots Creating files such as PDF, DOC, etc. Cycles or calculation tasks optimization
  • 3. CURL bots If you need parallel CURL downloads, you actually don't need multi-processing, because you can use curl_multi_* functions.
  • 4. 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.
  • 5. 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.
  • 6. 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.
  • 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'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.