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

More Related Content

Similar to Multi-tasking in PHP

Malware analysis
Malware analysisMalware analysis
Malware analysisxabean
 
Linux-HA with Pacemaker
Linux-HA with PacemakerLinux-HA with Pacemaker
Linux-HA with PacemakerKris Buytaert
 
Linux-HA with Pacemaker
Linux-HA with PacemakerLinux-HA with Pacemaker
Linux-HA with PacemakerKris Buytaert
 
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 ...DataWorks Summit/Hadoop Summit
 
Lisa12 methodologies
Lisa12 methodologiesLisa12 methodologies
Lisa12 methodologiesBrendan Gregg
 
MySQL HA with Pacemaker
MySQL HA with  PacemakerMySQL HA with  Pacemaker
MySQL HA with PacemakerKris Buytaert
 
Devopsdays se-2011
Devopsdays se-2011Devopsdays se-2011
Devopsdays se-2011lusis
 
Distributed Fuzzing Framework Design
Distributed Fuzzing Framework DesignDistributed Fuzzing Framework Design
Distributed Fuzzing Framework Designbannedit
 
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 062506ckuyehar
 
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...Nikolay Savvinov
 
Application Profiling for Memory and Performance
Application Profiling for Memory and PerformanceApplication Profiling for Memory and Performance
Application Profiling for Memory and Performancepradeepfn
 
Inside the Atlassian OnDemand Private Cloud
Inside the Atlassian OnDemand Private CloudInside the Atlassian OnDemand Private Cloud
Inside the Atlassian OnDemand Private CloudAtlassian
 
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 ApplicationsXiao Qin
 
2012-11-30-scalable game servers
2012-11-30-scalable game servers2012-11-30-scalable game servers
2012-11-30-scalable game serversWooga
 
Os Madsen Block
Os Madsen BlockOs Madsen Block
Os Madsen Blockoscon2007
 
Infrastructure Around Hadoop
Infrastructure Around HadoopInfrastructure Around Hadoop
Infrastructure Around HadoopDataWorks Summit
 

Similar to 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
 

More from Jonathon Hill

2019 SC Legislative Session Highlights
2019 SC Legislative Session Highlights2019 SC Legislative Session Highlights
2019 SC Legislative Session HighlightsJonathon Hill
 
DynamoDB in real life
DynamoDB in real lifeDynamoDB in real life
DynamoDB in real lifeJonathon 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 DisqusJonathon Hill
 
Estimation Protips - NCDevCon 2014
Estimation Protips - NCDevCon 2014Estimation Protips - NCDevCon 2014
Estimation Protips - NCDevCon 2014Jonathon Hill
 
Amazon: deal or debacle?
Amazon: deal or debacle?Amazon: deal or debacle?
Amazon: deal or debacle?Jonathon Hill
 

More from 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?
 

Recently uploaded

SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESmohitsingh558521
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxLoriGlavin3
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxBkGupta21
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rick Flair
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxLoriGlavin3
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 
Visualising and forecasting stocks using Dash
Visualising and forecasting stocks using DashVisualising and forecasting stocks using Dash
Visualising and forecasting stocks using Dashnarutouzumaki53779
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsNathaniel Shimoni
 
Sample pptx for embedding into website for demo
Sample pptx for embedding into website for demoSample pptx for embedding into website for demo
Sample pptx for embedding into website for demoHarshalMandlekar2
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
Ryan Mahoney - Will Artificial Intelligence Replace Real Estate Agents
Ryan Mahoney - Will Artificial Intelligence Replace Real Estate AgentsRyan Mahoney - Will Artificial Intelligence Replace Real Estate Agents
Ryan Mahoney - Will Artificial Intelligence Replace Real Estate AgentsRyan Mahoney
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterMydbops
 

Recently uploaded (20)

SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptx
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 
Visualising and forecasting stocks using Dash
Visualising and forecasting stocks using DashVisualising and forecasting stocks using Dash
Visualising and forecasting stocks using Dash
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directions
 
Sample pptx for embedding into website for demo
Sample pptx for embedding into website for demoSample pptx for embedding into website for demo
Sample pptx for embedding into website for demo
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
Ryan Mahoney - Will Artificial Intelligence Replace Real Estate Agents
Ryan Mahoney - Will Artificial Intelligence Replace Real Estate AgentsRyan Mahoney - Will Artificial Intelligence Replace Real Estate Agents
Ryan Mahoney - Will Artificial Intelligence Replace Real Estate Agents
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL Router
 

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