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!

46093583d8895095adb1b0071c505af2?s=128

Eric Mann

May 25, 2016
Tweet

Transcript

  1. I Promise() to Teach you Asynchronous PHP Eric Mann -

    Tozny - php[tek] 2016
  2. Terminology Imperative / Procedural Concurrent Parallel Concurrent Parallel

  3. Project Setup

  4. 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
  5. 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!
  6. 10 minute break

  7. 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
  8. 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();
  9. 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();
  10. Part II: Concurrent Let’s refactor for concurrency Again, there are

    5 TODOs in the application Now, let’s run it!
  11. 10 minute break

  12. 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
  13. pthreads <?php class WorkerThread extends Thread { private $workerId; public

    function __construct( $id ) { $this->workerId = $id; } public function run() { // Do some heavy processing } }
  14. 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(); }
  15. 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!
  16. Questions?

  17. I Promise() to Teach you Asynchronous PHP Eric Mann -

    Tozny - php[tek] 2016