The promise of asynchronous php

Embed Size (px)

Citation preview

1. The promise of asynchronous PHP Wim Godden Cu.be Solutions @wimgtr 2. Who am I ? Wim Godden (@wimgtr) 3. Where I'm from 4. Where I'm from 5. Where I'm from 6. Where I'm from 7. Where I'm from 8. Where I'm from 9. My town 10. My town 11. Belgium the traffic 12. Who am I ? Wim Godden (@wimgtr) Founder of Cu.be Solutions (http://cu.be) Open Source developer since 1997 Developer of OpenX, PHPCompatibility, PHPConsistent, ... Speaker at Open Source conferences 13. Who are you ? Developers ? Ever worked with asynchronous PHP libraries ? Node.JS ? 14. Synchronous processing 15. Asynchronous processing 16. Blocking I/O Disk reading/writing Network reading/writing Communication with DB (with some exceptions) Sending mail ... 17. Non-blocking = good Work on multiple things at same time Not entirely sequential anymore How do you know something is finished ? Events ! 18. Events Start Progress update End (successfully) Failed 19. Callback hell $one->do(function ($two) { $two->do(function ($three) { $three->do(function ($stillcounting) { $stillcounting->get(function() { throw new IQuitException(); }); }); }); }); 20. State of asynchronous PHP Several built-in functions Several libraries (using the built-in functions) Facebook Hack 21. Pthreads PECL extension Multithreading Requires zts (thread-safe) 22. 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("http://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(); var_dump($request->response); } 23. pcntl_fork Clones PHP process Multiprocessing, not multithreading No communication between processes No Apache 24. popen child.php