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

More Related Content

What's hot

SPL to the Rescue - Tek 09
SPL to the Rescue - Tek 09SPL to the Rescue - Tek 09
SPL to the Rescue - Tek 09
Elizabeth Smith
 
The Zen of Lithium
The Zen of LithiumThe Zen of Lithium
The Zen of Lithium
Nate Abele
 
The promise of asynchronous php
The promise of asynchronous phpThe promise of asynchronous php
The promise of asynchronous php
Wim 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 php
Sp 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
 
Intro to PHP
Intro to PHPIntro to PHP
Intro to PHP
Sandy Smith
 
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
 
Perl object ?
Perl object ?Perl object ?
Perl object ?
ℕicolas ℝ.
 
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-mostunknownparts
Bastian 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 Apps
Nate Abele
 
Doctrine fixtures
Doctrine fixturesDoctrine fixtures
Doctrine fixturesBill Chang
 

What's hot (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
 

Viewers also liked

Cinderella
CinderellaCinderella
Cinderella
acanete2
 
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-training
conline training
 
The hitchhiker's guide to PostgreSQL
The hitchhiker's guide to PostgreSQLThe hitchhiker's guide to PostgreSQL
The hitchhiker's guide to PostgreSQL
Federico 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 postgresql
Zaid Shabbir
 
Plpgsql russia-pgconf
Plpgsql russia-pgconfPlpgsql russia-pgconf
Plpgsql russia-pgconf
Pavel Stěhule
 
Trilliumbridgeehealthforum gautam
Trilliumbridgeehealthforum gautam Trilliumbridgeehealthforum gautam
Trilliumbridgeehealthforum gautam
gautam-neeraj
 
Erlang
ErlangErlang
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
Gerger
 
"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 Possible
Lukas 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 Databases
Markus Winand
 
Postgres
PostgresPostgres
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
Luminary Labs
 

Viewers also liked (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 to PHP CLI: A Cinderella Story

Quebec pdo
Quebec pdoQuebec pdo
Quebec pdo
Valentine Dianov
 
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
Tricode (part of Dept)
 
Fatc
FatcFatc
Symfony CMF - PHP Conference Brazil 2011
Symfony CMF - PHP Conference Brazil 2011Symfony CMF - PHP Conference Brazil 2011
Symfony CMF - PHP Conference Brazil 2011
Jacopo Romei
 
Leveraging Hadoop in your PostgreSQL Environment
Leveraging Hadoop in your PostgreSQL EnvironmentLeveraging Hadoop in your PostgreSQL Environment
Leveraging Hadoop in your PostgreSQL Environment
Jim Mlodgenski
 
Debugging: Rules & Tools
Debugging: Rules & ToolsDebugging: Rules & Tools
Debugging: Rules & Tools
Ian Barber
 
Facebook的缓存系统
Facebook的缓存系统Facebook的缓存系统
Facebook的缓存系统yiditushe
 
Building Testable PHP Applications
Building Testable PHP ApplicationsBuilding Testable PHP Applications
Building Testable PHP Applications
chartjes
 
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
PrestaShop
 
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 php
Wim Godden
 
Dmp hadoop getting_start
Dmp hadoop getting_startDmp hadoop getting_start
Dmp hadoop getting_start
Gim GyungJin
 
Express Presentation
Express PresentationExpress Presentation
Express Presentation
aaronheckmann
 
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
jaredhaight
 
Working with databases in Perl
Working with databases in PerlWorking with databases in Perl
Working with databases in Perl
Laurent Dami
 
Heavy Web Optimization: Backend
Heavy Web Optimization: BackendHeavy Web Optimization: Backend
Heavy Web Optimization: Backend
Võ 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 Web
Mikel 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 Version
Ian 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 2010
Binh Nguyen
 

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

Recently uploaded

GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
Sri Ambati
 
Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*
Frank van Harmelen
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Albert Hoitingh
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
OnBoard
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Product School
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
Product School
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
KatiaHIMEUR1
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
BookNet Canada
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Thierry Lestable
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
Product School
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
91mobiles
 
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered QualitySoftware Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Inflectra
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and back
Elena Simperl
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
James Anderson
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
Ana-Maria Mihalceanu
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
Alison B. Lowndes
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
Cheryl Hung
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
Elena Simperl
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
DianaGray10
 

Recently uploaded (20)

GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
 
Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
 
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered QualitySoftware Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and back
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
 

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.