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 ?



Neither of these are parallel execution

Parallel Execution

Parallel code is executed in more than one thread

Parallel != Asynchronous

pthreads != Posix Threads

Object Orientated


Safety Built In


100,000 installations from PECL

Compatible with most expectations


Any OS with a Posix Thread implementation

PHP 5.3+ built with Zend Thread Safety

Documentation on


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 ?




Co-existence is the only option !

What is included ?

Thread extends Threaded

Worker extends Thread



Safety !

A Simple Task

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(
        "", md5($i))));


Useful Things

Examples on github

Issues on github

Stackoverflow, Room 11

Parallel PHP

By Joe Watkins

Parallel PHP

Slides accompanying a parallel PHP talk

  • 2,796