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. I Promise() to Teach you
    Asynchronous PHP
    Eric Mann - Tozny - php[tek] 2016

    View Slide

  2. Terminology
    Imperative / Procedural
    Concurrent
    Parallel
    Concurrent Parallel

    View Slide

  3. Project Setup

    View Slide

  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

    View Slide

  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!

    View Slide

  6. 10 minute break

    View Slide

  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

    View Slide

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

    View Slide

  9. Icicle/ReactPHP
    $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();

    View Slide

  10. Part II: Concurrent
    Let’s refactor for concurrency
    Again, there are 5 TODOs in the application
    Now, let’s run it!

    View Slide

  11. 10 minute break

    View Slide

  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

    View Slide

  13. pthreads
    class WorkerThread extends Thread {
    private $workerId;
    public function __construct( $id ) {
    $this->workerId = $id;
    }
    public function run() {
    // Do some heavy processing
    }
    }

    View Slide

  14. pthreads
    $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();
    }

    View Slide

  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!

    View Slide

  16. Questions?

    View Slide

  17. I Promise() to Teach you
    Asynchronous PHP
    Eric Mann - Tozny - php[tek] 2016

    View Slide