Upgrade to Pro — share decks privately, control downloads, hide ads and more …

I Promise() to Teach You Asynchronous PHP

I Promise() to Teach You Asynchronous PHP

Asynchronous code sets JavaScript-powered projects apart from PHP. Thanks to some newer libraries, PHP can do the same tricks as Node on the server. This workshop will walk through the fundamentals of synchronous versus asynchronous execution. We will cover event loops in PHP and work through actual implementations of promises and co-routines. Finally, we will work through the steps required to convert an old-school synchronous application into a newfangled asynchronous beast!

Eric Mann

May 25, 2016
Tweet

More Decks by Eric Mann

Other Decks in Programming

Transcript

  1. Copy the USB drive to your machine Install Vagrant/VirtualBox if

    you don’t have them Add the eisago Vagrant box: 
 vagrant box add ericmann/eisago eisago.box Start the Vagrant box and SSH into it Set up your IDE for the project
  2. Part I: Imperative We’re building an importer - turn a

    collection of text files into an indexed MongoDB database Let’s work through the 5 TODOs in the application Now, let’s run it!
  3. Icicle/ReactPHP Write asynchronous (concurrent) code in PHP Support event loops

    natively (and stop blocking I/O) Use Promises in PHP to juggle multiple tasks at once Requires an event (and signaling) extension for PHP to work properly
  4. Icicle/ReactPHP <?php $promise = new Promise( function( $resolve, $reject )

    { do_something_async()->then( $resolve ); } ); $promise ->then( function( $data ) { echo $data; } )->done ( function() { echo 'Done!'; } ); Loop\run();
  5. Icicle/ReactPHP <?php $promise1 = new Promise( function( $resolve, $reject )

    { do_something_async()->then( $resolve ); } ); $promise2 = new Promise( function( $resolve, $reject ) { do_something_else_async()->then( $resolve ); } ); Icicle\Awaitable\all( [ $promise1, $promise2 ] ) ->done( function() { echo 'Done!'; }, function() { echo 'Error!'; } ); Loop\run();
  6. Part II: Concurrent Let’s refactor for concurrency Again, there are

    5 TODOs in the application Now, let’s run it!
  7. pthreads User-land multi-threading in PHP by way of an extension

    Provides threads, workers, and threaded objects All of your code – the server and the worker – are the same application Each component has its own scope
  8. pthreads <?php class WorkerThread extends Thread { private $workerId; public

    function __construct( $id ) { $this->workerId = $id; } public function run() { // Do some heavy processing } }
  9. pthreads <?php $workers = []; foreach ( range( 0, 5

    ) as $id ) { $worker = new WorkerThread( $id ); $workers[] = $worker; $worker->start(); // Now they'll each run independently } foreach ( range( 0, 5 ) as $id ) {
 // Force the threads to rejoin the main process synchronously $workers[ $id ]->join(); }
  10. Part III: Concurrent Parallel Let’s refactor for parallelism (threading) Yet

    again, there are 5 TODOs in the application
 They’re in 2 different files, though … Now, let’s run it!