SlideShare une entreprise Scribd logo
1  sur  37
Multi-tasking in PHP



Wednesday, December 5, 12
About Me
                   •        Formerly CTO for
                            Company52

                   •        Currently work at
                            Brandmovers on
                            Northside Drive

                   •        Self-taught

                   •        Full-time geek since
                            2007




Wednesday, December 5, 12
Wednesday, December 5, 12
Use Cases
                •      System resources are
                       not the bottleneck
                •      Batch processing
                       involving an API:
                      •     E-Mail
                      •     Geocoding
                      •     Electronic billing
                •      Daemons


Wednesday, December 5, 12
Alternatives

                   • Gearman
                   • curl_multi_*
                   • Other scripting languages


Wednesday, December 5, 12
Theory



Wednesday, December 5, 12
Two ways to multi-task
                             Multi-processing        Multi-threading

                             Separate memory          Shared memory

                             Errors are isolated   Errors are not isolated

                            Separate permissions     Same permissions

                                Linux/UNIX               Windows


Wednesday, December 5, 12
Multiprocessing                                     Multithreading

             Process 1        program
                                                                Process 1          program



                                                                  Thread 1
                 state:         virtual
                                              memory
                PC, stack     processor                              state:          virtual
                                                                    PC, stack      processor


             Process 2        program                             Thread 2
                                                                     state:          virtual
                                                                    PC, stack      processor
                                                                                                 memory
                 state:         virtual
                                              memory
                PC, stack     processor


                                                                  Thread m
                                                                     state:          virtual
             Process n        program
                                                                    PC, stack      processor




                 state:         virtual
                                              memory
                PC, stack     processor
                                                                Process n


                                               Courtesy www.fmc-modeling.org

Wednesday, December 5, 12
Multiprocessing

                   • “The simultaneous execution of two or
                            more programs by separate CPUs under
                            integrated control.”
                   • Clones the entire process, except resources
                   • Copy-on-write memory

Wednesday, December 5, 12
Forking




                            Diagram courtesy cnx.org



Wednesday, December 5, 12
Child Process

                   • A cloned copy of a parent process
                   • Receives a new process ID and a parent
                            process ID
                   • Does some work
                   • Dies

Wednesday, December 5, 12
...sort of.




                            Photo Credit: Christopher Brian (2011 Toronto Zombie Walk)



Wednesday, December 5, 12
Parent Responsibilities

                   • Reproduction
                   • Monitors child process status
                   • “Reap” zombie processes


Wednesday, December 5, 12
Process Signals
                             Signal     Description
                            SIGCHLD   Child process died

                            SIGINT     User Interrupt

                            SIGTERM       Terminate

                            SIGKILL   Forcibly terminate



Wednesday, December 5, 12
PHP Implementation



Wednesday, December 5, 12
Requirements
                   •        Unix-like operating system
                   •        PHP 4.1+
                   •        PHP PCNTL extension
                            (compile with --enable-pcntl)
                   •        PHP Semaphore extension, optional
                            (--enable-sysvsem, --enable-sysvshm, --enable-sysvmsg)
                   •        Plenty of memory
                   •        Multiple CPU cores



Wednesday, December 5, 12
Overview
                   1. Define signal handlers
                   2. Fetch a dataset
                   3. Fork off one child process for each item
                   4. Stop forking when a threshold is reached, and sleep
                   5. Reap a child process whenever SIGCHLD is received
                   6. If there’s more work to do, fork more processes
                   7. When all child processes have been reaped,
                      terminate


Wednesday, December 5, 12
declare(ticks = 1);

                 // Setup our signal handlers
                 pcntl_signal(SIGTERM, "signal_handler");
                 pcntl_signal(SIGINT,  "signal_handler");
                 pcntl_signal(SIGCHLD, "signal_handler");




Wednesday, December 5, 12
function signal_handler($signal)
                 {
                 	

 switch ($signal)
                 	

 {
                 	

 	

 case SIGINT:
                 	

 	

 case SIGTERM:
                 	

 	

 	

 // kill all child processes
                 	

 	

 	

 exit(0);
                 	

 	

 case SIGCHLD:
                 	

 	

 	

 // reap a child process
                 	

 	

 	

 reap_child();
                 	

 	

 break;
                 	

 }
                 }


Wednesday, December 5, 12
$pid = pcntl_fork();

                  switch($pid)
                  {
                  	

 case 0:
                  	

 	

 // Child process
                  	

 	

 call_user_func($callback, $data);
                  	

 	

 posix_kill(posix_getppid(), SIGCHLD);
                  	

 	

 exit;
                  	

 case -1:
                  	

 	

 // Parent process, fork failed
                  	

 	

 throw new Exception("Out of memory!");
                  	

 default:
                  	

 	

 // Parent process, fork succeeded
                  	

 	

 $processes[$pid] = TRUE;
                  }

Wednesday, December 5, 12
Repeat for
                            each unit of work


Wednesday, December 5, 12
function reap_child()
             {
             	

 // Check if any child process has terminated,
             	

 // and if so remove it from memory
             	

 $pid = pcntl_wait($status, WNOHANG);
             	

 if ($pid < 0)
             	

 {
             	

 	

 throw new Exception("Out of memory");
             	

 }
             	

 elseif ($pid > 0)
             	

 {
             	

 	

 unset($processes[$pid]);
             	

 }	

             }

Wednesday, December 5, 12
Demo Time!
                            http://gist.github.com/4212160




Wednesday, December 5, 12
Wednesday, December 5, 12
DON’T:
                   •        DON’T fork a web process (CLI only!)
                   •        DON’T overload your system
                   •        DON’T open resources before forking
                   •        DO respect common POSIX signals
                   •        DO remove zombie processes
                   •        DO force new database connections in children
                            mysql_reconnect($s, $u, $p, TRUE);


Wednesday, December 5, 12
Challenges



Wednesday, December 5, 12
Race Conditions
                   •        A logic bug where the result is affected by the
                            sequence or timing of uncontrollable events
                   •        Adding debug logic can change timing
                   •        Dirty reads
                   •        Lost data
                   •        Unpredictable behavior
                   •        Deadlocks, hanging, crashing


Wednesday, December 5, 12
Wednesday, December 5, 12
Wednesday, December 5, 12
Solutions

                        • Handle I/O in the parent process
                            exclusively
                        • Manage resources with semaphores
                            and/or mutexes




Wednesday, December 5, 12
Semaphores

                   • Semaphore = atomically updated counter
                   • Mutex = binary semaphore with ownership
                   • PHP: sem_get(), sem_release()
                   • Transactional databases use semaphores

Wednesday, December 5, 12
Deadlocks




                                        Image credit: csunplugged.org




Wednesday, December 5, 12
Bonus Slides



Wednesday, December 5, 12
Shared Memory
                   •        Advanced inter-process communication

                   •        Pass data back to the parent process

                   •        PHP Shared Memory extension
                            (--enable-shmop)

                   •        PHP System V Shared Memory extension
                            (--enable-sysvshm)

                        •     More robust

                        •     Compatible with other languages


Wednesday, December 5, 12
Daemonization

                   • Fork, kill parent
                   • Orphaned child process continues running
                   • Signal and error handling are critical
                   • Server daemons usually fork child
                            processes to handle requests



Wednesday, December 5, 12
Wednesday, December 5, 12
Thank You!


                              @compwright
                            http://bit.ly/atlphpm




Wednesday, December 5, 12

Contenu connexe

Similaire à Multi-tasking in PHP

Devopsdays se-2011
Devopsdays se-2011Devopsdays se-2011
Devopsdays se-2011
lusis
 
Hadoop - Disk Fail In Place (DFIP)
Hadoop - Disk Fail In Place (DFIP)Hadoop - Disk Fail In Place (DFIP)
Hadoop - Disk Fail In Place (DFIP)
mundlapudi
 
VistA GT.M & Linux Security 062506
VistA GT.M & Linux Security 062506VistA GT.M & Linux Security 062506
VistA GT.M & Linux Security 062506
ckuyehar
 
An Active and Hybrid Storage System for Data-intensive Applications
An Active and Hybrid Storage System for Data-intensive ApplicationsAn Active and Hybrid Storage System for Data-intensive Applications
An Active and Hybrid Storage System for Data-intensive Applications
Xiao Qin
 
2012-11-30-scalable game servers
2012-11-30-scalable game servers2012-11-30-scalable game servers
2012-11-30-scalable game servers
Wooga
 
Os Madsen Block
Os Madsen BlockOs Madsen Block
Os Madsen Block
oscon2007
 
Infrastructure Around Hadoop
Infrastructure Around HadoopInfrastructure Around Hadoop
Infrastructure Around Hadoop
DataWorks Summit
 

Similaire à Multi-tasking in PHP (20)

Malware analysis
Malware analysisMalware analysis
Malware analysis
 
Linux-HA with Pacemaker
Linux-HA with PacemakerLinux-HA with Pacemaker
Linux-HA with Pacemaker
 
Linux-HA with Pacemaker
Linux-HA with PacemakerLinux-HA with Pacemaker
Linux-HA with Pacemaker
 
How to overcome mysterious problems caused by large and multi-tenancy Hadoop ...
How to overcome mysterious problems caused by large and multi-tenancy Hadoop ...How to overcome mysterious problems caused by large and multi-tenancy Hadoop ...
How to overcome mysterious problems caused by large and multi-tenancy Hadoop ...
 
You suck at Memory Analysis
You suck at Memory AnalysisYou suck at Memory Analysis
You suck at Memory Analysis
 
Lisa12 methodologies
Lisa12 methodologiesLisa12 methodologies
Lisa12 methodologies
 
MySQL HA with Pacemaker
MySQL HA with  PacemakerMySQL HA with  Pacemaker
MySQL HA with Pacemaker
 
Devopsdays se-2011
Devopsdays se-2011Devopsdays se-2011
Devopsdays se-2011
 
Distributed Fuzzing Framework Design
Distributed Fuzzing Framework DesignDistributed Fuzzing Framework Design
Distributed Fuzzing Framework Design
 
Hadoop - Disk Fail In Place (DFIP)
Hadoop - Disk Fail In Place (DFIP)Hadoop - Disk Fail In Place (DFIP)
Hadoop - Disk Fail In Place (DFIP)
 
Hadoop, Taming Elephants
Hadoop, Taming ElephantsHadoop, Taming Elephants
Hadoop, Taming Elephants
 
VistA GT.M & Linux Security 062506
VistA GT.M & Linux Security 062506VistA GT.M & Linux Security 062506
VistA GT.M & Linux Security 062506
 
Using the big guns: Advanced OS performance tools for troubleshooting databas...
Using the big guns: Advanced OS performance tools for troubleshooting databas...Using the big guns: Advanced OS performance tools for troubleshooting databas...
Using the big guns: Advanced OS performance tools for troubleshooting databas...
 
Application Profiling for Memory and Performance
Application Profiling for Memory and PerformanceApplication Profiling for Memory and Performance
Application Profiling for Memory and Performance
 
Inside the Atlassian OnDemand Private Cloud
Inside the Atlassian OnDemand Private CloudInside the Atlassian OnDemand Private Cloud
Inside the Atlassian OnDemand Private Cloud
 
The Accidental DBA
The Accidental DBAThe Accidental DBA
The Accidental DBA
 
An Active and Hybrid Storage System for Data-intensive Applications
An Active and Hybrid Storage System for Data-intensive ApplicationsAn Active and Hybrid Storage System for Data-intensive Applications
An Active and Hybrid Storage System for Data-intensive Applications
 
2012-11-30-scalable game servers
2012-11-30-scalable game servers2012-11-30-scalable game servers
2012-11-30-scalable game servers
 
Os Madsen Block
Os Madsen BlockOs Madsen Block
Os Madsen Block
 
Infrastructure Around Hadoop
Infrastructure Around HadoopInfrastructure Around Hadoop
Infrastructure Around Hadoop
 

Plus de Jonathon Hill

Building for success and failure with Disqus
Building for success and failure with DisqusBuilding for success and failure with Disqus
Building for success and failure with Disqus
Jonathon Hill
 

Plus de Jonathon Hill (6)

2019 SC Legislative Session Highlights
2019 SC Legislative Session Highlights2019 SC Legislative Session Highlights
2019 SC Legislative Session Highlights
 
DynamoDB in real life
DynamoDB in real lifeDynamoDB in real life
DynamoDB in real life
 
Building for success and failure with Disqus
Building for success and failure with DisqusBuilding for success and failure with Disqus
Building for success and failure with Disqus
 
Estimation Protips - NCDevCon 2014
Estimation Protips - NCDevCon 2014Estimation Protips - NCDevCon 2014
Estimation Protips - NCDevCon 2014
 
Estimation Protips
Estimation ProtipsEstimation Protips
Estimation Protips
 
Amazon: deal or debacle?
Amazon: deal or debacle?Amazon: deal or debacle?
Amazon: deal or debacle?
 

Dernier

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 

Dernier (20)

Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 

Multi-tasking in PHP

  • 2. About Me • Formerly CTO for Company52 • Currently work at Brandmovers on Northside Drive • Self-taught • Full-time geek since 2007 Wednesday, December 5, 12
  • 4. Use Cases • System resources are not the bottleneck • Batch processing involving an API: • E-Mail • Geocoding • Electronic billing • Daemons Wednesday, December 5, 12
  • 5. Alternatives • Gearman • curl_multi_* • Other scripting languages Wednesday, December 5, 12
  • 7. Two ways to multi-task Multi-processing Multi-threading Separate memory Shared memory Errors are isolated Errors are not isolated Separate permissions Same permissions Linux/UNIX Windows Wednesday, December 5, 12
  • 8. Multiprocessing Multithreading Process 1 program Process 1 program Thread 1 state: virtual memory PC, stack processor state: virtual PC, stack processor Process 2 program Thread 2 state: virtual PC, stack processor memory state: virtual memory PC, stack processor Thread m state: virtual Process n program PC, stack processor state: virtual memory PC, stack processor Process n Courtesy www.fmc-modeling.org Wednesday, December 5, 12
  • 9. Multiprocessing • “The simultaneous execution of two or more programs by separate CPUs under integrated control.” • Clones the entire process, except resources • Copy-on-write memory Wednesday, December 5, 12
  • 10. Forking Diagram courtesy cnx.org Wednesday, December 5, 12
  • 11. Child Process • A cloned copy of a parent process • Receives a new process ID and a parent process ID • Does some work • Dies Wednesday, December 5, 12
  • 12. ...sort of. Photo Credit: Christopher Brian (2011 Toronto Zombie Walk) Wednesday, December 5, 12
  • 13. Parent Responsibilities • Reproduction • Monitors child process status • “Reap” zombie processes Wednesday, December 5, 12
  • 14. Process Signals Signal Description SIGCHLD Child process died SIGINT User Interrupt SIGTERM Terminate SIGKILL Forcibly terminate Wednesday, December 5, 12
  • 16. Requirements • Unix-like operating system • PHP 4.1+ • PHP PCNTL extension (compile with --enable-pcntl) • PHP Semaphore extension, optional (--enable-sysvsem, --enable-sysvshm, --enable-sysvmsg) • Plenty of memory • Multiple CPU cores Wednesday, December 5, 12
  • 17. Overview 1. Define signal handlers 2. Fetch a dataset 3. Fork off one child process for each item 4. Stop forking when a threshold is reached, and sleep 5. Reap a child process whenever SIGCHLD is received 6. If there’s more work to do, fork more processes 7. When all child processes have been reaped, terminate Wednesday, December 5, 12
  • 18. declare(ticks = 1); // Setup our signal handlers pcntl_signal(SIGTERM, "signal_handler"); pcntl_signal(SIGINT,  "signal_handler"); pcntl_signal(SIGCHLD, "signal_handler"); Wednesday, December 5, 12
  • 19. function signal_handler($signal) { switch ($signal) { case SIGINT: case SIGTERM: // kill all child processes exit(0); case SIGCHLD: // reap a child process reap_child(); break; } } Wednesday, December 5, 12
  • 20. $pid = pcntl_fork(); switch($pid) { case 0: // Child process call_user_func($callback, $data); posix_kill(posix_getppid(), SIGCHLD); exit; case -1: // Parent process, fork failed throw new Exception("Out of memory!"); default: // Parent process, fork succeeded $processes[$pid] = TRUE; } Wednesday, December 5, 12
  • 21. Repeat for each unit of work Wednesday, December 5, 12
  • 22. function reap_child() { // Check if any child process has terminated, // and if so remove it from memory $pid = pcntl_wait($status, WNOHANG); if ($pid < 0) { throw new Exception("Out of memory"); } elseif ($pid > 0) { unset($processes[$pid]); } } Wednesday, December 5, 12
  • 23. Demo Time! http://gist.github.com/4212160 Wednesday, December 5, 12
  • 25. DON’T: • DON’T fork a web process (CLI only!) • DON’T overload your system • DON’T open resources before forking • DO respect common POSIX signals • DO remove zombie processes • DO force new database connections in children mysql_reconnect($s, $u, $p, TRUE); Wednesday, December 5, 12
  • 27. Race Conditions • A logic bug where the result is affected by the sequence or timing of uncontrollable events • Adding debug logic can change timing • Dirty reads • Lost data • Unpredictable behavior • Deadlocks, hanging, crashing Wednesday, December 5, 12
  • 30. Solutions • Handle I/O in the parent process exclusively • Manage resources with semaphores and/or mutexes Wednesday, December 5, 12
  • 31. Semaphores • Semaphore = atomically updated counter • Mutex = binary semaphore with ownership • PHP: sem_get(), sem_release() • Transactional databases use semaphores Wednesday, December 5, 12
  • 32. Deadlocks Image credit: csunplugged.org Wednesday, December 5, 12
  • 34. Shared Memory • Advanced inter-process communication • Pass data back to the parent process • PHP Shared Memory extension (--enable-shmop) • PHP System V Shared Memory extension (--enable-sysvshm) • More robust • Compatible with other languages Wednesday, December 5, 12
  • 35. Daemonization • Fork, kill parent • Orphaned child process continues running • Signal and error handling are critical • Server daemons usually fork child processes to handle requests Wednesday, December 5, 12
  • 37. Thank You! @compwright http://bit.ly/atlphpm Wednesday, December 5, 12