SlideShare una empresa de Scribd logo
1 de 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

Más contenido relacionado

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

Más de 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
 

Más 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?
 

Último

Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Victor Rentea
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesrafiqahmad00786416
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024The Digital Insurer
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistandanishmna97
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxRustici Software
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWERMadyBayot
 
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.pdfsudhanshuwaghmare1
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Angeliki Cooney
 
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 DiscoveryTrustArc
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKSpring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKJago de Vreede
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusZilliz
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...Zilliz
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Zilliz
 

Último (20)

Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
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
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
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
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKSpring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 

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