SlideShare una empresa de Scribd logo
1 de 39
PHP CLI A Cinderella Story
Introduction ,[object Object],[object Object]
If you use Windows... ...please leave now.
Overview ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Why Background Processing ,[object Object],[object Object],[object Object],[object Object]
Why Use PHP? ,[object Object],[object Object],[object Object],[object Object],[object Object]
Identifying Suitable Processes ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Single Process ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Introducing the CLI SAPI ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Writing a cronjob ,[object Object],[object Object],[object Object],[object Object],[object Object]
Overrun protection ,[object Object],[object Object]
Work Queues ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
MySQL Work Queues ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
SQLite Work Queue ,[object Object],[object Object],[object Object],[object Object]
Memcached ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Persistent Processing ,[object Object],[object Object],[object Object],[object Object],[object Object]
Process Control ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Signals ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Daemonize ,[object Object]
Multiple Processes ,[object Object],[object Object],[object Object],[object Object],[object Object]
Directed vs. Autonomous ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Forking <?php $pid  =  pcntl_fork (); if ( $pid  == - 1 ) {     die( &quot;Could not fork!&quot; ); } else if ( $pid ) {      // parent } else {      // child } ?>
Forking Multiple Children <?php define ( 'MAX_CHILDREN' ,  5 ); $children  = array(); $jobs  =  get_jobs (); while ( count ( $jobs )) {   if ( count ( $children ) <  MAX_CHILDREN ) {      $data  =  array_shift ( $jobs );      $pid  =  pcntl_fork ();     if ( $pid  == - 1 ) {       die( &quot;Could not fork!&quot; );     } else if ( $pid ) {        $children [ $pid ] =  true ;     } else {        process_data ( $data );       exit( 0 );     }   }   while ( $wait_pid  =  pcntl_waitpid (- 1 ,  $status ,  WNOHANG )) {     if ( $wait_pid  == - 1 ) {       die( &quot;problem in pcntl_waitpid!&quot; );     }     unset( $children [ $wait_pid ]);   } } ?>
Shared Resources ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Shared Resources <?php // ... // bad time to open a database connection $db  = new  PDO ( 'mysql:host=localhost' ,  'dbuser' ,  'pass' ); while ( count ( $data )) {   if ( count ( $children ) <  MAX_CHILDREN ) {      $data  =  array_shift ( $jobs );      $pid  =  pcntl_fork ();     if ( $pid  == - 1 ) {       die( &quot;Could not fork!&quot; );     } else if ( $pid ) {        $children [ $pid ] =  true ;     } else {        process_data ( $data ,  $db );       exit( 0 );  // When the child exits the database connection                 // will be disposed of.      }   }    // ... } ?>
Shared Resources <?php // ... while ( count ( $data )) {   if ( count ( $children ) <  MAX_CHILDREN ) {      $data  =  array_shift ( $jobs );      $pid  =  pcntl_fork ();     if ( $pid  == - 1 ) {       die( &quot;Could not fork!&quot; );     } else if ( $pid ) {        $children [ $pid ] =  true ;     } else {       // Much safer       $db  = new  PDO ( 'mysql:host=localhost' ,  'dbuser' ,  'pass' );        process_data ( $data ,  $db );       exit( 0 );  // When the child exits the database connection                 // will be disposed of.      }   }    // ... } ?>
Memory Usage ,[object Object],[object Object],[object Object],[object Object]
Memory Usage <?php define ( 'MAX_CHILDREN' ,  5 ); $children  = array(); $jobs  =  get_jobs (); while ( count ( $jobs )) {   if ( count ( $children ) <  MAX_CHILDREN ) {      $data  =  array_shift ( $jobs );      $pid  =  pcntl_fork ();     if ( $pid  == - 1 ) {       die( &quot;Could not fork!&quot; );     } else if ( $pid ) {        $children [ $pid ] =  true ;     } else {        unset ( $jobs ); // <--- will save memory in your child where you do not need $jobs around anymore        process_data ( $data );       exit( 0 );     }   }   while ( $wait_pid  =  pcntl_waitpid (- 1 ,  $status ,  WNOHANG )) {     if ( $wait_pid  == - 1 ) {       die( &quot;problem in pcntl_waitpid!&quot; );     }     unset( $children [ $wait_pid ]);   } } ?>
Shared Memory ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
How to Talk to Your Kids ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
How to Talk to Your Kids ,[object Object],[object Object],[object Object]
How to Talk to Your Kids <?php $socks  =  stream_socket_pair ( STREAM_PF_UNIX ,  STREAM_SOCK_STREAM ,  STREAM_IPPROTO_IP ); $pid  =  pcntl_fork (); if ( $pid  == - 1 ) {      die( 'could not fork!' ); } else if ( $pid ) {       // parent      fclose ( $socks [ 1 ]);      fwrite ( $socks [ 0 ],  &quot;Hi kid&quot; );     echo  fgets ( $socks [ 0 ]);      fclose ( $socks [ 0 ]); } else {      // child      fclose ( $socks [ 0 ]);      fwrite ( $socks [ 1 ],  &quot;Hi parent&quot; );     echo  fgets ( $socks [ 1 ]);      fclose ( $socks [ 1 ]); } /* Output:  Hi kid Hi parent */ ?>
Distributing Across Servers ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Locking ,[object Object],[object Object],[object Object],[object Object]
Talking to Your Servers ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Failure Tolerance ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
&quot;Angel&quot; Process ,[object Object]
Angel as a Cron Job ,[object Object],[object Object],[object Object]
Resources ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]

Más contenido relacionado

La actualidad más candente

SPL to the Rescue - Tek 09
SPL to the Rescue - Tek 09SPL to the Rescue - Tek 09
SPL to the Rescue - Tek 09Elizabeth Smith
 
The Zen of Lithium
The Zen of LithiumThe Zen of Lithium
The Zen of LithiumNate Abele
 
The promise of asynchronous php
The promise of asynchronous phpThe promise of asynchronous php
The promise of asynchronous phpWim Godden
 
4069180 Caching Performance Lessons From Facebook
4069180 Caching Performance Lessons From Facebook4069180 Caching Performance Lessons From Facebook
4069180 Caching Performance Lessons From Facebookguoqing75
 
Intro to php
Intro to phpIntro to php
Intro to phpSp Singh
 
Dependency injection in PHP 5.3/5.4
Dependency injection in PHP 5.3/5.4Dependency injection in PHP 5.3/5.4
Dependency injection in PHP 5.3/5.4Fabien Potencier
 
Decouple Your Code For Reusability (International PHP Conference / IPC 2008)
Decouple Your Code For Reusability (International PHP Conference / IPC 2008)Decouple Your Code For Reusability (International PHP Conference / IPC 2008)
Decouple Your Code For Reusability (International PHP Conference / IPC 2008)Fabien Potencier
 
Unit and Functional Testing with Symfony2
Unit and Functional Testing with Symfony2Unit and Functional Testing with Symfony2
Unit and Functional Testing with Symfony2Fabien Potencier
 
Php unit the-mostunknownparts
Php unit the-mostunknownpartsPhp unit the-mostunknownparts
Php unit the-mostunknownpartsBastian Feder
 
2011-03-29 London - drools
2011-03-29 London - drools2011-03-29 London - drools
2011-03-29 London - droolsGeoffrey De Smet
 
Dependency Injection with PHP and PHP 5.3
Dependency Injection with PHP and PHP 5.3Dependency Injection with PHP and PHP 5.3
Dependency Injection with PHP and PHP 5.3Fabien Potencier
 
international PHP2011_Bastian Feder_jQuery's Secrets
international PHP2011_Bastian Feder_jQuery's Secretsinternational PHP2011_Bastian Feder_jQuery's Secrets
international PHP2011_Bastian Feder_jQuery's Secretssmueller_sandsmedia
 
From mysql to MongoDB(MongoDB2011北京交流会)
From mysql to MongoDB(MongoDB2011北京交流会)From mysql to MongoDB(MongoDB2011北京交流会)
From mysql to MongoDB(MongoDB2011北京交流会)Night Sailer
 
Building Lithium Apps
Building Lithium AppsBuilding Lithium Apps
Building Lithium AppsNate Abele
 
Doctrine fixtures
Doctrine fixturesDoctrine fixtures
Doctrine fixturesBill Chang
 

La actualidad más candente (20)

SPL to the Rescue - Tek 09
SPL to the Rescue - Tek 09SPL to the Rescue - Tek 09
SPL to the Rescue - Tek 09
 
The Zen of Lithium
The Zen of LithiumThe Zen of Lithium
The Zen of Lithium
 
The promise of asynchronous php
The promise of asynchronous phpThe promise of asynchronous php
The promise of asynchronous php
 
jQuery secrets
jQuery secretsjQuery secrets
jQuery secrets
 
4069180 Caching Performance Lessons From Facebook
4069180 Caching Performance Lessons From Facebook4069180 Caching Performance Lessons From Facebook
4069180 Caching Performance Lessons From Facebook
 
Intro to php
Intro to phpIntro to php
Intro to php
 
Dependency injection in PHP 5.3/5.4
Dependency injection in PHP 5.3/5.4Dependency injection in PHP 5.3/5.4
Dependency injection in PHP 5.3/5.4
 
Intro to PHP
Intro to PHPIntro to PHP
Intro to PHP
 
Decouple Your Code For Reusability (International PHP Conference / IPC 2008)
Decouple Your Code For Reusability (International PHP Conference / IPC 2008)Decouple Your Code For Reusability (International PHP Conference / IPC 2008)
Decouple Your Code For Reusability (International PHP Conference / IPC 2008)
 
Perl object ?
Perl object ?Perl object ?
Perl object ?
 
Agile Memcached
Agile MemcachedAgile Memcached
Agile Memcached
 
Unit and Functional Testing with Symfony2
Unit and Functional Testing with Symfony2Unit and Functional Testing with Symfony2
Unit and Functional Testing with Symfony2
 
Php unit the-mostunknownparts
Php unit the-mostunknownpartsPhp unit the-mostunknownparts
Php unit the-mostunknownparts
 
2011-03-29 London - drools
2011-03-29 London - drools2011-03-29 London - drools
2011-03-29 London - drools
 
Symfony2 - WebExpo 2010
Symfony2 - WebExpo 2010Symfony2 - WebExpo 2010
Symfony2 - WebExpo 2010
 
Dependency Injection with PHP and PHP 5.3
Dependency Injection with PHP and PHP 5.3Dependency Injection with PHP and PHP 5.3
Dependency Injection with PHP and PHP 5.3
 
international PHP2011_Bastian Feder_jQuery's Secrets
international PHP2011_Bastian Feder_jQuery's Secretsinternational PHP2011_Bastian Feder_jQuery's Secrets
international PHP2011_Bastian Feder_jQuery's Secrets
 
From mysql to MongoDB(MongoDB2011北京交流会)
From mysql to MongoDB(MongoDB2011北京交流会)From mysql to MongoDB(MongoDB2011北京交流会)
From mysql to MongoDB(MongoDB2011北京交流会)
 
Building Lithium Apps
Building Lithium AppsBuilding Lithium Apps
Building Lithium Apps
 
Doctrine fixtures
Doctrine fixturesDoctrine fixtures
Doctrine fixtures
 

Destacado

Cinderella
CinderellaCinderella
Cinderellaacanete2
 
Stored Procedures com PostgreSQL: porque usar.
Stored Procedures com PostgreSQL:  porque usar.Stored Procedures com PostgreSQL:  porque usar.
Stored Procedures com PostgreSQL: porque usar.Atmos Maciel
 
Bn 1016 demo postgre sql-online-training
Bn 1016 demo  postgre sql-online-trainingBn 1016 demo  postgre sql-online-training
Bn 1016 demo postgre sql-online-trainingconline training
 
The hitchhiker's guide to PostgreSQL
The hitchhiker's guide to PostgreSQLThe hitchhiker's guide to PostgreSQL
The hitchhiker's guide to PostgreSQLFederico Campoli
 
Beautiful PHP CLI Scripts
Beautiful PHP CLI ScriptsBeautiful PHP CLI Scripts
Beautiful PHP CLI ScriptsJesse Donat
 
No sql bigdata and postgresql
No sql bigdata and postgresqlNo sql bigdata and postgresql
No sql bigdata and postgresqlZaid Shabbir
 
Trilliumbridgeehealthforum gautam
Trilliumbridgeehealthforum gautam Trilliumbridgeehealthforum gautam
Trilliumbridgeehealthforum gautam gautam-neeraj
 
PostgreSQL for Oracle Developers and DBA's
PostgreSQL for Oracle Developers and DBA'sPostgreSQL for Oracle Developers and DBA's
PostgreSQL for Oracle Developers and DBA'sGerger
 
"PostgreSQL для разработчиков приложений", Павел Лузанов, (Постгрес Профессио...
"PostgreSQL для разработчиков приложений", Павел Лузанов, (Постгрес Профессио..."PostgreSQL для разработчиков приложений", Павел Лузанов, (Постгрес Профессио...
"PostgreSQL для разработчиков приложений", Павел Лузанов, (Постгрес Профессио...Badoo Development
 
Cinderella Short Story
Cinderella Short StoryCinderella Short Story
Cinderella Short StoryEltari
 
10 SQL Tricks that You Didn't Think Were Possible
10 SQL Tricks that You Didn't Think Were Possible10 SQL Tricks that You Didn't Think Were Possible
10 SQL Tricks that You Didn't Think Were PossibleLukas Eder
 
Modern SQL in Open Source and Commercial Databases
Modern SQL in Open Source and Commercial DatabasesModern SQL in Open Source and Commercial Databases
Modern SQL in Open Source and Commercial DatabasesMarkus Winand
 
Hype vs. Reality: The AI Explainer
Hype vs. Reality: The AI ExplainerHype vs. Reality: The AI Explainer
Hype vs. Reality: The AI ExplainerLuminary Labs
 

Destacado (18)

Cinderella script
Cinderella scriptCinderella script
Cinderella script
 
Cinderella
CinderellaCinderella
Cinderella
 
Stored Procedures com PostgreSQL: porque usar.
Stored Procedures com PostgreSQL:  porque usar.Stored Procedures com PostgreSQL:  porque usar.
Stored Procedures com PostgreSQL: porque usar.
 
Bn 1016 demo postgre sql-online-training
Bn 1016 demo  postgre sql-online-trainingBn 1016 demo  postgre sql-online-training
Bn 1016 demo postgre sql-online-training
 
The hitchhiker's guide to PostgreSQL
The hitchhiker's guide to PostgreSQLThe hitchhiker's guide to PostgreSQL
The hitchhiker's guide to PostgreSQL
 
Beautiful PHP CLI Scripts
Beautiful PHP CLI ScriptsBeautiful PHP CLI Scripts
Beautiful PHP CLI Scripts
 
No sql bigdata and postgresql
No sql bigdata and postgresqlNo sql bigdata and postgresql
No sql bigdata and postgresql
 
Plpgsql russia-pgconf
Plpgsql russia-pgconfPlpgsql russia-pgconf
Plpgsql russia-pgconf
 
Trilliumbridgeehealthforum gautam
Trilliumbridgeehealthforum gautam Trilliumbridgeehealthforum gautam
Trilliumbridgeehealthforum gautam
 
Erlang
ErlangErlang
Erlang
 
PostgreSQL for Oracle Developers and DBA's
PostgreSQL for Oracle Developers and DBA'sPostgreSQL for Oracle Developers and DBA's
PostgreSQL for Oracle Developers and DBA's
 
"PostgreSQL для разработчиков приложений", Павел Лузанов, (Постгрес Профессио...
"PostgreSQL для разработчиков приложений", Павел Лузанов, (Постгрес Профессио..."PostgreSQL для разработчиков приложений", Павел Лузанов, (Постгрес Профессио...
"PostgreSQL для разработчиков приложений", Павел Лузанов, (Постгрес Профессио...
 
Cinderella Short Story
Cinderella Short StoryCinderella Short Story
Cinderella Short Story
 
10 SQL Tricks that You Didn't Think Were Possible
10 SQL Tricks that You Didn't Think Were Possible10 SQL Tricks that You Didn't Think Were Possible
10 SQL Tricks that You Didn't Think Were Possible
 
Modern SQL in Open Source and Commercial Databases
Modern SQL in Open Source and Commercial DatabasesModern SQL in Open Source and Commercial Databases
Modern SQL in Open Source and Commercial Databases
 
Postgres
PostgresPostgres
Postgres
 
Statutes and codes of HTTP
Statutes and codes of HTTPStatutes and codes of HTTP
Statutes and codes of HTTP
 
Hype vs. Reality: The AI Explainer
Hype vs. Reality: The AI ExplainerHype vs. Reality: The AI Explainer
Hype vs. Reality: The AI Explainer
 

Similar a PHP CLI: A Cinderella Story

Zend framework 03 - singleton factory data mapper caching logging
Zend framework 03 - singleton factory data mapper caching loggingZend framework 03 - singleton factory data mapper caching logging
Zend framework 03 - singleton factory data mapper caching loggingTricode (part of Dept)
 
Symfony CMF - PHP Conference Brazil 2011
Symfony CMF - PHP Conference Brazil 2011Symfony CMF - PHP Conference Brazil 2011
Symfony CMF - PHP Conference Brazil 2011Jacopo Romei
 
Leveraging Hadoop in your PostgreSQL Environment
Leveraging Hadoop in your PostgreSQL EnvironmentLeveraging Hadoop in your PostgreSQL Environment
Leveraging Hadoop in your PostgreSQL EnvironmentJim Mlodgenski
 
Debugging: Rules & Tools
Debugging: Rules & ToolsDebugging: Rules & Tools
Debugging: Rules & ToolsIan Barber
 
Facebook的缓存系统
Facebook的缓存系统Facebook的缓存系统
Facebook的缓存系统yiditushe
 
Building Testable PHP Applications
Building Testable PHP ApplicationsBuilding Testable PHP Applications
Building Testable PHP Applicationschartjes
 
Good practices for PrestaShop code security and optimization
Good practices for PrestaShop code security and optimizationGood practices for PrestaShop code security and optimization
Good practices for PrestaShop code security and optimizationPrestaShop
 
SparkR - Play Spark Using R (20160909 HadoopCon)
SparkR - Play Spark Using R (20160909 HadoopCon)SparkR - Play Spark Using R (20160909 HadoopCon)
SparkR - Play Spark Using R (20160909 HadoopCon)wqchen
 
The promise of asynchronous php
The promise of asynchronous phpThe promise of asynchronous php
The promise of asynchronous phpWim Godden
 
Dmp hadoop getting_start
Dmp hadoop getting_startDmp hadoop getting_start
Dmp hadoop getting_startGim GyungJin
 
Express Presentation
Express PresentationExpress Presentation
Express Presentationaaronheckmann
 
Get-Help: An intro to PowerShell and how to Use it for Evil
Get-Help: An intro to PowerShell and how to Use it for EvilGet-Help: An intro to PowerShell and how to Use it for Evil
Get-Help: An intro to PowerShell and how to Use it for Eviljaredhaight
 
Working with databases in Perl
Working with databases in PerlWorking with databases in Perl
Working with databases in PerlLaurent Dami
 
Heavy Web Optimization: Backend
Heavy Web Optimization: BackendHeavy Web Optimization: Backend
Heavy Web Optimization: BackendVõ Duy Tuấn
 
Charla EHU Noviembre 2014 - Desarrollo Web
Charla EHU Noviembre 2014 - Desarrollo WebCharla EHU Noviembre 2014 - Desarrollo Web
Charla EHU Noviembre 2014 - Desarrollo WebMikel Torres Ugarte
 
Debugging: Rules And Tools - PHPTek 11 Version
Debugging: Rules And Tools - PHPTek 11 VersionDebugging: Rules And Tools - PHPTek 11 Version
Debugging: Rules And Tools - PHPTek 11 VersionIan Barber
 
Introduction to windows power shell in sharepoint 2010
Introduction to windows power shell in sharepoint 2010Introduction to windows power shell in sharepoint 2010
Introduction to windows power shell in sharepoint 2010Binh Nguyen
 

Similar a PHP CLI: A Cinderella Story (20)

Quebec pdo
Quebec pdoQuebec pdo
Quebec pdo
 
Zend framework 03 - singleton factory data mapper caching logging
Zend framework 03 - singleton factory data mapper caching loggingZend framework 03 - singleton factory data mapper caching logging
Zend framework 03 - singleton factory data mapper caching logging
 
Fatc
FatcFatc
Fatc
 
Symfony CMF - PHP Conference Brazil 2011
Symfony CMF - PHP Conference Brazil 2011Symfony CMF - PHP Conference Brazil 2011
Symfony CMF - PHP Conference Brazil 2011
 
Leveraging Hadoop in your PostgreSQL Environment
Leveraging Hadoop in your PostgreSQL EnvironmentLeveraging Hadoop in your PostgreSQL Environment
Leveraging Hadoop in your PostgreSQL Environment
 
Building Custom PHP Extensions
Building Custom PHP ExtensionsBuilding Custom PHP Extensions
Building Custom PHP Extensions
 
Debugging: Rules & Tools
Debugging: Rules & ToolsDebugging: Rules & Tools
Debugging: Rules & Tools
 
Facebook的缓存系统
Facebook的缓存系统Facebook的缓存系统
Facebook的缓存系统
 
Building Testable PHP Applications
Building Testable PHP ApplicationsBuilding Testable PHP Applications
Building Testable PHP Applications
 
Good practices for PrestaShop code security and optimization
Good practices for PrestaShop code security and optimizationGood practices for PrestaShop code security and optimization
Good practices for PrestaShop code security and optimization
 
SparkR - Play Spark Using R (20160909 HadoopCon)
SparkR - Play Spark Using R (20160909 HadoopCon)SparkR - Play Spark Using R (20160909 HadoopCon)
SparkR - Play Spark Using R (20160909 HadoopCon)
 
The promise of asynchronous php
The promise of asynchronous phpThe promise of asynchronous php
The promise of asynchronous php
 
Dmp hadoop getting_start
Dmp hadoop getting_startDmp hadoop getting_start
Dmp hadoop getting_start
 
Express Presentation
Express PresentationExpress Presentation
Express Presentation
 
Get-Help: An intro to PowerShell and how to Use it for Evil
Get-Help: An intro to PowerShell and how to Use it for EvilGet-Help: An intro to PowerShell and how to Use it for Evil
Get-Help: An intro to PowerShell and how to Use it for Evil
 
Working with databases in Perl
Working with databases in PerlWorking with databases in Perl
Working with databases in Perl
 
Heavy Web Optimization: Backend
Heavy Web Optimization: BackendHeavy Web Optimization: Backend
Heavy Web Optimization: Backend
 
Charla EHU Noviembre 2014 - Desarrollo Web
Charla EHU Noviembre 2014 - Desarrollo WebCharla EHU Noviembre 2014 - Desarrollo Web
Charla EHU Noviembre 2014 - Desarrollo Web
 
Debugging: Rules And Tools - PHPTek 11 Version
Debugging: Rules And Tools - PHPTek 11 VersionDebugging: Rules And Tools - PHPTek 11 Version
Debugging: Rules And Tools - PHPTek 11 Version
 
Introduction to windows power shell in sharepoint 2010
Introduction to windows power shell in sharepoint 2010Introduction to windows power shell in sharepoint 2010
Introduction to windows power shell in sharepoint 2010
 

Último

Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphNeo4j
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
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 AutomationSafe Software
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 

Último (20)

Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
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
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 

PHP CLI: A Cinderella Story

  • 1. PHP CLI A Cinderella Story
  • 2.
  • 3. If you use Windows... ...please leave now.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22. Forking <?php $pid  =  pcntl_fork (); if ( $pid  == - 1 ) {     die( &quot;Could not fork!&quot; ); } else if ( $pid ) {      // parent } else {      // child } ?>
  • 23. Forking Multiple Children <?php define ( 'MAX_CHILDREN' ,  5 ); $children  = array(); $jobs  =  get_jobs (); while ( count ( $jobs )) {   if ( count ( $children ) <  MAX_CHILDREN ) {      $data  =  array_shift ( $jobs );      $pid  =  pcntl_fork ();     if ( $pid  == - 1 ) {       die( &quot;Could not fork!&quot; );     } else if ( $pid ) {        $children [ $pid ] =  true ;     } else {        process_data ( $data );       exit( 0 );     }   }   while ( $wait_pid  =  pcntl_waitpid (- 1 ,  $status ,  WNOHANG )) {     if ( $wait_pid  == - 1 ) {       die( &quot;problem in pcntl_waitpid!&quot; );     }     unset( $children [ $wait_pid ]);   } } ?>
  • 24.
  • 25. Shared Resources <?php // ... // bad time to open a database connection $db  = new  PDO ( 'mysql:host=localhost' ,  'dbuser' ,  'pass' ); while ( count ( $data )) {   if ( count ( $children ) <  MAX_CHILDREN ) {      $data  =  array_shift ( $jobs );      $pid  =  pcntl_fork ();     if ( $pid  == - 1 ) {       die( &quot;Could not fork!&quot; );     } else if ( $pid ) {        $children [ $pid ] =  true ;     } else {        process_data ( $data ,  $db );       exit( 0 );  // When the child exits the database connection                // will be disposed of.      }   }   // ... } ?>
  • 26. Shared Resources <?php // ... while ( count ( $data )) {   if ( count ( $children ) <  MAX_CHILDREN ) {      $data  =  array_shift ( $jobs );      $pid  =  pcntl_fork ();     if ( $pid  == - 1 ) {       die( &quot;Could not fork!&quot; );     } else if ( $pid ) {        $children [ $pid ] =  true ;     } else {       // Much safer       $db  = new  PDO ( 'mysql:host=localhost' ,  'dbuser' ,  'pass' );       process_data ( $data ,  $db );       exit( 0 );  // When the child exits the database connection                // will be disposed of.      }   }   // ... } ?>
  • 27.
  • 28. Memory Usage <?php define ( 'MAX_CHILDREN' ,  5 ); $children  = array(); $jobs  =  get_jobs (); while ( count ( $jobs )) {   if ( count ( $children ) <  MAX_CHILDREN ) {      $data  =  array_shift ( $jobs );      $pid  =  pcntl_fork ();     if ( $pid  == - 1 ) {       die( &quot;Could not fork!&quot; );     } else if ( $pid ) {        $children [ $pid ] =  true ;     } else {       unset ( $jobs ); // <--- will save memory in your child where you do not need $jobs around anymore        process_data ( $data );       exit( 0 );     }   }   while ( $wait_pid  =  pcntl_waitpid (- 1 ,  $status ,  WNOHANG )) {     if ( $wait_pid  == - 1 ) {       die( &quot;problem in pcntl_waitpid!&quot; );     }     unset( $children [ $wait_pid ]);   } } ?>
  • 29.
  • 30.
  • 31.
  • 32. How to Talk to Your Kids <?php $socks  =  stream_socket_pair ( STREAM_PF_UNIX ,  STREAM_SOCK_STREAM ,  STREAM_IPPROTO_IP ); $pid =  pcntl_fork (); if ( $pid  == - 1 ) {      die( 'could not fork!' ); } else if ( $pid ) {      // parent      fclose ( $socks [ 1 ]);     fwrite ( $socks [ 0 ],  &quot;Hi kid&quot; );     echo  fgets ( $socks [ 0 ]);     fclose ( $socks [ 0 ]); } else {      // child      fclose ( $socks [ 0 ]);     fwrite ( $socks [ 1 ],  &quot;Hi parent&quot; );     echo  fgets ( $socks [ 1 ]);     fclose ( $socks [ 1 ]); } /* Output: Hi kid Hi parent */ ?>
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.