Asynchronous I/O in PHP

7,582 views

Published on

Published in: Technology, Design
  • Be the first to comment

Asynchronous I/O in PHP

  1. 1. IPC Spring 2013IPC Spring 2013Asynchronous I/O in PHP
  2. 2. About MeAbout Me● Thomas Weinert● papaya Software GmbH● papaya CMS● @ThomasWeinert● http://www.a-basketful-of-papayas.net
  3. 3. Synchronous I/OSynchronous I/O● Request resource 1● Wait for resource 1● Use resource 1● Request resource 2● Wait for resource 2● Use resource 2
  4. 4. Asynchronous I/OAsynchronous I/O● Request resource 1● Define usage of resource 1● Request Resource 2● Define usage of resource 2● Wait for resource 1 and 2 are used
  5. 5. Event LoopEvent LoopListenersLoopExternalProcessExternalProcessFile
  6. 6. Browser / JavascriptBrowser / Javascript<html><head><title>Loop</title></head><body><div id="output"></div><script type="text/javascript">var e = document.getElementById(output);var counter = 0;var interval = window.setInterval(function() {e.textContent = e.textContent +counter.toString() + , ;counter++;},1000);</script></body></html>
  7. 7. PHPPHP<?phpinclude(../src/Carica/Io/Loader.php);CaricaIoLoader::register();use CaricaIoEventLoop;$loop = LoopFactory::get();$i = 0;$loop->setInterval(function () use (&$i) {echo $i++;},1000);$loop->run();
  8. 8. LibrariesLibraries● Carica Io● https://bitbucket.org/ThomasWeinert/carica-io● ReactPHP● http://reactphp.org/● LibEvent (ext/libevent)
  9. 9. Event EmitterEvent EmitterObjectEventCallbackCallbackEventCallbackEvent Event● Attach● on(), once()● Trigger● emit()
  10. 10. Event Emitter ExampleEvent Emitter Example<?phpinclude(../../src/Carica/Io/Loader.php);CaricaIoLoader::register();$server = new CaricaIoNetworkServer();$server->events()->on(connection,function ($stream) {...});$server->listen(8080);CaricaIoEventLoopFactory::run();
  11. 11. CallbacksCallbacks● list of handlers for one event$foo = new stdClass();$foo->literal = ;$callbacks = new CaricaIoCallbacks();$callbacks->add(function () use ($foo) {$foo->literal .= Hello ;})->add(function () use ($foo) {$foo->literal .= World!;});$callbacks();echo $foo->literal;
  12. 12. Deferred/PromiseDeferred/Promise● „An object that acts as a proxy for a result that is initially unknown, usuallybecause the computation of its value is yet incomplete.“ (Wikipedia)● http://domenic.me/2012/10/14/youre-missing-the-point-of-promises/● jQuery $.Deferred● ReactPHP/Promise● https://github.com/reactphp/promise/● CaricaIoPromise
  13. 13. jQueryjQuery<html><head><title>Promise</title></head><body><div id="output"></div><scripttype="text/javascript"src="http://code.jquery.com/jquery-1.9.1.js"></script><script type="text/javascript">jQuery(function () {jQuery.get(hello-world.xml).done(function (xml) {$(#output).text($(data, xml).text());});});</script></body></html>
  14. 14. MethodsMethods● Deferred/Promise● done()● fail()● always()● progress()● state()● ...
  15. 15. State ChangeState Change● Deferred● reject()● resolve()● notify
  16. 16. ChainingChaining● Deferred::when()->then()● All resolved => done()● First failed => fail()
  17. 17. MySQL PreparationMySQL Preparation<?phpinclude(../../src/Carica/Io/Loader.php);CaricaIoLoader::register();use CaricaIo;$mysqlOne = new IoDeferredMySQL(new mysqli(localhost));$mysqlTwo = new IoDeferredMySQL(new mysqli(localhost));$time = microtime(TRUE);...
  18. 18. MySQLMySQL...$queries = IoDeferred::When($mysqlOne("SELECT Query 1, SLEEP(5)")->done(function($result) use ($time) {var_dump(iterator_to_array($result));var_dump(microtime(TRUE) - $time);}),$mysqlTwo("SELECT Query 2, SLEEP(1)")->done(function($result) use ($time) {var_dump(iterator_to_array($result));var_dump(microtime(TRUE) - $time);}));$loop = IoEventLoopFactory::get();IoEventLoopFactory::run($queries);
  19. 19. ArduinoArduino
  20. 20. Arduino NanoArduino Nano
  21. 21. Carica FirmataCarica Firmata● Serial Port Communication● https://bitbucket.org/ThomasWeinert/carica-firmata
  22. 22. Arduino DemosArduino Demos● Blink● Dimmer● Rest● Color Wheel

×