The promise of asynchronous php

Embed Size (px)

Citation preview

The promise of asynchronous PHP

Wim GoddenCu.be Solutions@wimgtr

Who am I ?

Wim Godden (@wimgtr)

Where I'm from

Where I'm from

Where I'm from

Where I'm from

Where I'm from

Where I'm from

My town

My town

Belgium the traffic

Who am I ?

Wim Godden (@wimgtr)

Founder of Cu.be Solutions (http://cu.be)

Open Source developer since 1997

Developer of PHPCompatibility, OpenX, PHPConsistent, ...

Speaker at Open Source conferences

Who are you ?

Developers ?

Ever worked with asynchronous PHP libraries ?

Node.JS ?

Synchronous processing

InsertorderCheckpaymentUpdatestockFedexAPISendmailThankyoupage

Asynchronous processing

InsertorderCheckpaymentUpdatestockFedexAPISendmailThankyoupage

Blocking I/O

Disk reading/writing

Network reading/writing

Communication with DB (with some exceptions)

Sending mail

...

Non-blocking = good

Work on multiple things at same time

Not entirely sequential anymore

How do you know something is finished ? Events !

Events

Start

Progress update

End (successfully)

Failed

Callback hell

$one->do(function ($two) { $two->do(function ($three) { $three->do(function ($stillcounting) { $stillcounting->get(function() { throw new IQuitException(); }); }); });});

State of asynchronous PHP

Several built-in functions

Several libraries (using the built-in functions)

Facebook Hack

Pthreads

PECL extension

Multithreading

Requires zts (thread-safe)

Pthreads

class WebRequest extends Thread { public $url; public $response; public function __construct($url){ $this->url = $url; } public function run() { $this->response = file_get_contents($this->url); }}

$request = new WebRequest("https://cu.be");

if ($request->start()) { /* do some work here */ $a = array_fill(0, 10000000, 'test'); for ($i = 0; $i < count($a); $i++) {}

/* ensure we have data */ $request->join(); echo $request->response;}

pcntl_fork

Forks PHP process

Multiprocessing, not multithreading

No communication between processes

No Apache

popen

child.php