Asynchronous I/O in PHP
Upcoming SlideShare
Loading in...5
×
 

Asynchronous I/O in PHP

on

  • 4,096 views

 

Statistics

Views

Total Views
4,096
Views on SlideShare
4,082
Embed Views
14

Actions

Likes
1
Downloads
16
Comments
0

1 Embed 14

http://librosweb.es 14

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Asynchronous I/O in PHP Asynchronous I/O in PHP Presentation Transcript

  • IPC Spring 2013IPC Spring 2013Asynchronous I/O in PHP
  • About MeAbout Me● Thomas Weinert● papaya Software GmbH● papaya CMS● @ThomasWeinert● http://www.a-basketful-of-papayas.net
  • 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
  • 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
  • Event LoopEvent LoopListenersLoopExternalProcessExternalProcessFile
  • 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>
  • 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();
  • LibrariesLibraries● Carica Io● https://bitbucket.org/ThomasWeinert/carica-io● ReactPHP● http://reactphp.org/● LibEvent (ext/libevent)
  • Event EmitterEvent EmitterObjectEventCallbackCallbackEventCallbackEvent Event● Attach● on(), once()● Trigger● emit()
  • 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();
  • 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;
  • 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
  • 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>
  • MethodsMethods● Deferred/Promise● done()● fail()● always()● progress()● state()● ...
  • State ChangeState Change● Deferred● reject()● resolve()● notify
  • ChainingChaining● Deferred::when()->then()● All resolved => done()● First failed => fail()
  • 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);...
  • 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);
  • ArduinoArduino
  • Arduino NanoArduino Nano
  • Carica FirmataCarica Firmata● Serial Port Communication● https://bitbucket.org/ThomasWeinert/carica-firmata
  • Arduino DemosArduino Demos● Blink● Dimmer● Rest● Color Wheel