Parallel PHP

Joe Watkins

Who am I ?

In my third year of being a core contributor to PHP

Wrote pthreads, before being involved in internals

Since, wrote many extensions, APCu, uopz, JITFu, strict

One of the two initial developers of phpdbg

Went on a date with Phil Sturgeon once

I live and breathe internals, every day

What do we mean by parallel ?

Synchronous

Asynchronous

Neither of these are parallel execution

Parallel Execution

Parallel code is executed in more than one thread

Parallel != Asynchronous

pthreads != Posix Threads

Object Orientated

Modern-ish

Safety Built In

Mature

100,000 installations from PECL

Compatible with most expectations

Requirements

Any OS with a Posix Thread implementation

PHP 5.3+ built with Zend Thread Safety

Documentation on php.net

Patience

Even More Patience

What not to do !

Front controllers do not need threading

Template engines do not need threading

Web requests do not need threading

Exhaust all avenues before making things wantonly complicated

Multi-threading is hard.

Do not be scared

What to do ?

Isolation

Isolation

Isolation

Co-existence is the only option !

What is included ?

Thread extends Threaded

Worker extends Thread

Threaded

Pool

Safety !

A Simple Task

<?php
class Client extends Threaded {

    public function __construct($url) {
        $this->url = $url;
    }

    public function run() {
        $this->response = file_get_contents($this->url);
    }
    
    public $url;
    public $response;
}

$pool = new Pool(4);

while (@$i++ < 10) {
    $pool->submit(new Client(sprintf(
        "http://google.com/?q=%s", md5($i))));
}

$pool->shutdown();
?>

Useful Things

http://php.net/pthreads

http://github.com/krakjoe/pthreads

Examples on github

Issues on github

Stackoverflow, Room 11

krakjoe@php.net

Parallel PHP

By Joe Watkins

Parallel PHP

Slides accompanying a parallel PHP talk

  • 631
Loading comments...