Asynchronous I/O in PHP

22
IPC Spring 2013 IPC Spring 2013 Asynchronous I/O in PHP

Transcript of Asynchronous I/O in PHP

Page 1: Asynchronous I/O in PHP

IPC Spring 2013IPC Spring 2013

Asynchronous I/O in PHP

Page 2: Asynchronous I/O in PHP

About MeAbout Me● Thomas Weinert

● papaya Software GmbH● papaya CMS

● @ThomasWeinert● http://www.a-basketful-of-papayas.net

Page 3: Asynchronous I/O in PHP

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

Page 4: Asynchronous I/O in PHP

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

Page 5: Asynchronous I/O in PHP

Event LoopEvent Loop

Listeners

Loop

External ProcessExternal ProcessFile

Page 6: Asynchronous I/O in PHP

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>

Page 7: Asynchronous I/O in PHP

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();

Page 8: Asynchronous I/O in PHP

LibrariesLibraries● Carica Io

● https://bitbucket.org/ThomasWeinert/carica-io

● ReactPHP● http://reactphp.org/

● LibEvent (ext/libevent)

Page 9: Asynchronous I/O in PHP

Event EmitterEvent Emitter

Object

Event

Callback

Callback

Event

Callback

Event Event● Attach

● on(), once()● Trigger

● emit()

Page 10: Asynchronous I/O in PHP

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();

Page 11: Asynchronous I/O in PHP

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;

Page 12: Asynchronous I/O in PHP

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

Page 13: Asynchronous I/O in PHP

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>

Page 14: Asynchronous I/O in PHP

MethodsMethods● Deferred/Promise

● done()● fail()● always()● progress()● state()● ...

Page 15: Asynchronous I/O in PHP

State ChangeState Change● Deferred

● reject()● resolve()● notify

Page 16: Asynchronous I/O in PHP

ChainingChaining● Deferred::when()->then()● All resolved => done()● First failed => fail()

Page 17: Asynchronous I/O in PHP

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);

...

Page 18: Asynchronous I/O in PHP

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);

Page 19: Asynchronous I/O in PHP

ArduinoArduino

Page 20: Asynchronous I/O in PHP

Arduino NanoArduino Nano

Page 21: Asynchronous I/O in PHP

Carica FirmataCarica Firmata● Serial Port Communication● https://bitbucket.org/ThomasWeinert/carica-

firmata

Page 22: Asynchronous I/O in PHP

Arduino DemosArduino Demos● Blink● Dimmer● Rest● Color Wheel