Asynchronous I/O in PHP
-
Upload
thomas-weinert -
Category
Technology
-
view
5.031 -
download
4
Transcript of Asynchronous I/O in PHP
IPC Spring 2013IPC Spring 2013
Asynchronous 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 Loop
Listeners
Loop
External ProcessExternal ProcessFile
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');Carica\Io\Loader::register();use Carica\Io\Event\Loop;
$loop = Loop\Factory::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 Emitter
Object
Event
Callback
Callback
Event
Callback
Event Event● Attach
● on(), once()● Trigger
● emit()
Event Emitter ExampleEvent Emitter Example<?phpinclude('../../src/Carica/Io/Loader.php');Carica\Io\Loader::register();
$server = new Carica\Io\Network\Server();$server->events()->on( 'connection', function ($stream) { ... });
$server->listen(8080);
Carica\Io\Event\Loop\Factory::run();
CallbacksCallbacks● list of handlers for one event$foo = new \stdClass();$foo->literal = '';$callbacks = new \Carica\Io\Callbacks();$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, usually
because 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/● Carica\Io\Promise
jQueryjQuery<html> <head><title>Promise</title></head> <body> <div id="output"></div> <script type="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');Carica\Io\Loader::register();
use Carica\Io;
$mysqlOne = new Io\Deferred\MySQL(new mysqli('localhost'));$mysqlTwo = new Io\Deferred\MySQL(new mysqli('localhost'));
$time = microtime(TRUE);
...
MySQLMySQL...$queries = Io\Deferred::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 = Io\Event\Loop\Factory::get();Io\Event\Loop\Factory::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