Slide 1

Slide 1 text

Parallel Processing in PHP Fork It ! Nathaniel McHugh Monday, 8 October 12

Slide 2

Slide 2 text

• Software Engineer Inviqa • @natmchugh • http://fishtrap.co.uk • likes doing useless PHP Monday, 8 October 12

Slide 3

Slide 3 text

Monday, 8 October 12

Slide 4

Slide 4 text

Pasta or Monday, 8 October 12

Slide 5

Slide 5 text

0 1 2 3 4 5 6 7 8 9 10 1 cup 2 cups 3 cups 4 cups t (minutes) kettle hob microwave Monday, 8 October 12

Slide 6

Slide 6 text

Monday, 8 October 12

Slide 7

Slide 7 text

Number of Processors https://github.com/natmchugh/PHP-Num- Processors php > echo num_processors_available(); 2 php > echo num_processors_configured(); 2 Monday, 8 October 12

Slide 8

Slide 8 text

Forking • php has no multi threading • pcntl extension • *nix only • cli only really Monday, 8 October 12

Slide 9

Slide 9 text

fork() Monday, 8 October 12

Slide 10

Slide 10 text

fork() $ ps -a | grep ps 3394 ttys003 0:00.00 ps -a 3395 ttys003 0:00.00 grep ps $ Monday, 8 October 12

Slide 11

Slide 11 text

fork() $ ps -a | grep ps 3394 ttys003 0:00.00 ps -a 3395 ttys003 0:00.00 grep ps $ $ man fork | grep returns Upon successful completion, fork() returns a value of 0 to the child process and returns the process ID of the child process to the parent Monday, 8 October 12

Slide 12

Slide 12 text

Monday, 8 October 12

Slide 13

Slide 13 text

Concurrency real 1m10.322s real 0m39.928s real 0m45.827s Monday, 8 October 12

Slide 14

Slide 14 text

Die Hard PHP Daemons doSomeWork(); } Monday, 8 October 12

Slide 15

Slide 15 text

From the Beginning Monday, 8 October 12

Slide 16

Slide 16 text

From the Beginning $ php start_with_manual.php $ Monday, 8 October 12

Slide 17

Slide 17 text

Slide 18

Slide 18 text

Slide 19

Slide 19 text

• Zombies are dead • Orphans are children whose parent has died Zombies vs Orphans Monday, 8 October 12

Slide 20

Slide 20 text

multiple children?

Slide 21

Slide 21 text

multiple children?

Slide 22

Slide 22 text

multiple children?

Slide 23

Slide 23 text

multiple children?

Slide 24

Slide 24 text

The Pattern require('deep_thought.php'); $deepThought = new DeepThought(); $children = array(); for ($i=0; $i < 5; $i++) { $children[] = $pid = pcntl_fork(); if ($pid == -1) { die('could not fork'); } else if (0 === $pid) { $lifeUniverseEverthing = $deepThought->findTheAnswer(); echo "Child $i says:",$lifeUniverseEverthing,PHP_EOL; die(); } } do { $pid = pcntl_wait($status); $children = array_diff($children, array($pid)); } while (count($children) > 0); Monday, 8 October 12

Slide 25

Slide 25 text

The Pattern require('deep_thought.php'); $deepThought = new DeepThought(); $children = array(); for ($i=0; $i < 5; $i++) { $children[] = $pid = pcntl_fork(); if ($pid == -1) { die('could not fork'); } else if (0 === $pid) { $lifeUniverseEverthing = $deepThought->findTheAnswer(); echo "Child $i says:",$lifeUniverseEverthing,PHP_EOL; die(); } } do { $pid = pcntl_wait($status); $children = array_diff($children, array($pid)); } while (count($children) > 0); $ time php the_pattern.php Child 0 says:42 Child 3 says:42 Child 1 says:Child 2 says:42 42 Child 4 says:42 real 0m5.453s user 0m0.056s sys 0m0.060s $ Monday, 8 October 12

Slide 26

Slide 26 text

Signals

Slide 27

Slide 27 text

Signals

Slide 28

Slide 28 text

SIGCHLD 0) { $children = array_diff($children, array($pid)); echo "Child $pid collected".PHP_EOL; } } Monday, 8 October 12

Slide 29

Slide 29 text

SIGCHLD 0) { $children = array_diff($children, array($pid)); echo "Child $pid collected".PHP_EOL; } } php sigchld.php ....Child 0 says:42 Child 2 says:42 Child 3 says:42 Child 1 says:42 Child 4 says:42 .Caught SIGCHLD Child 22877 collected Child 22876 collected Child 22874 collected .Caught SIGCHLD Child 22875 collected .Caught SIGCHLD Child 22878 collected Monday, 8 October 12

Slide 30

Slide 30 text

Process Isolation

Slide 31

Slide 31 text

Process Isolation

Slide 32

Slide 32 text

Resources

Slide 33

Slide 33 text

Resources $ php resources.php Parent Child $ php resources.php Child Parent $

Slide 34

Slide 34 text

How do you get your children to talk to you? Monday, 8 October 12

Slide 35

Slide 35 text

Listening to your children • DB • files • memcached, apc or shared memory • sockets Monday, 8 October 12

Slide 36

Slide 36 text

Sockets Example findTheAnswer(); writeToSocket($data, $sockets[0]); die(); } } do { $pid = pcntl_wait($status); $forks = array_diff($forks, array($pid)); $sockets = $socketPairs[$pid]; $data = readFromSocket($sockets[1]); var_dump($data); } while (count($forks) > 0); Monday, 8 October 12

Slide 37

Slide 37 text

Next Level • Gearman • Message Queues ØMQ, RabbitMQ .. Monday, 8 October 12

Slide 38

Slide 38 text

Thinking Parallel “It turns out that what was difficult, and almost impossible, is to take an ordinary program and automatically figure out how to use the parallel computation effectively...” Richard P. Feynman Monday, 8 October 12

Slide 39

Slide 39 text

Input Processing Output Input Input Input Processing Processing Output Output Output Monday, 8 October 12

Slide 40

Slide 40 text

• Even Distribution of work /input • Size of messages • Data Dependancies Monday, 8 October 12

Slide 41

Slide 41 text

PHPLOC input: file system output: array of stats processing: tokenize and count occurrences Monday, 8 October 12

Slide 42

Slide 42 text

PHPLOC array(1781) { [0] => string(51) "zf2/bin/autoload_example.php" [1] => string(52) "zf2/bin/ autoload_examples.php" [2] => string(53) "zf2/bin/ classmap_generator.php" [3] => string(60) "zf2/bin/ createAutoloadTestClasses.php" [4] => string(54) "zf2/bin/ pluginmap_generator.php" ... foreach ($files as $key => $file) { $directory = dirname($file); if (!isset($this->directories[$directory])) { $this->directories[$directory] = TRUE; } ... Monday, 8 October 12

Slide 43

Slide 43 text

PHPLOC real 0m7.216s user 0m6.492s sys 0m0.240s $ time phploc --cores 1 zf2 $ time phploc --cores 2 zf2 real 0m5.500s user 0m6.686s sys 0m0.293s https://github.com/natmchugh/phploc Monday, 8 October 12

Slide 44

Slide 44 text

Amdahls Law Monday, 8 October 12

Slide 45

Slide 45 text

Embarrassingly Parallel Monday, 8 October 12

Slide 46

Slide 46 text

Mandelbrot Set Input: Pixel co-ordinates Processing: Hard core number crunching Output: An image Monday, 8 October 12

Slide 47

Slide 47 text

Dividing Work https://github.com/natmchugh/PHP-Fractals Monday, 8 October 12

Slide 48

Slide 48 text

Thanks Joind.in: http://joind.in/7002 @natmchugh http://fishtrap.co.uk Monday, 8 October 12

Slide 49

Slide 49 text

• Front Fork http://www.flickr.com/photos/de_atienza/ • Paint, Laptop & fork Lift http://consumeconsume.com/ • Haynes Manual http://www.flickr.com/photos/8490341@N04/ • Zombie Child http://www.flickr.com/photos/zenobia_joy/ • Signal http://www.flickr.com/photos/rbrwr/ • Pattern http://www.flickr.com/photos/eadaoinflynn Monday, 8 October 12