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

Async PHP Redux

Async PHP Redux

Christopher Pitt

May 08, 2014
Tweet

More Decks by Christopher Pitt

Other Decks in Technology

Transcript

  1. the problem! ! ›❯ scaling php from the inside is

    hard ›❯ persistent connections halt execution ›❯ synchronous core and history
  2. synchronous requests! ! http request hits server → then you

    parse input → then you read the database → then you render the view → then you return the response
  3. asynchronous requests! ! http request hits server → then you

    ask for parsed input ! you get parsed input → then you ask for a database read ! ...
  4. fixing the problem! ! ›❯ does it need to be

    fixed? ›❯ hardware is cheaper than developers
  5. ›❯ composer require "illuminate/queue:4.1.0" ./composer.json has been updated Loading composer

    repositories with package information Updating dependencies (including require-dev) - Installing symfony/process (v2.4.4) Loading from cache
  6. $queue = new Illuminate\Queue\Capsule\Manager(); ! $queue->addConnection([ "driver" => "beanstalkd", "host"

    => "localhost", "queue" => "default" ]); ! $queue->push("SendEmail",["message" => $message]);
  7. class SendEmail { public function fire($job, $data) { // process

    the job // mark the job as handled with $job->release() } }
  8. learn more! ! ›❯ queues in laravel http://laravel.com/docs/queues ›❯ illuminate/queue

    https://github.com/illuminate/queue ›❯ beanstalkd http://kr.github.io/beanstalkd ›❯ rabbit mq https://www.rabbitmq.com ›❯ iron mq http://www.iron.io/mq ›❯ amazon sqs https://aws.amazon.com/sqs
  9. how it works! ! client connects to daemon → then

    client adds task to queue → then daemon sends task to worker → then worker processes task → then worker sends result to daemon → then daemon sends result to client
  10. ›❯ brew install gearmand ==> Downloading https://launchpad.net/gearmand/1.2/1.1.9/+download/ gearmand-1.1.9.tar.gz Already downloaded:

    /Library/Caches/Homebrew/gearman-1.1.9.tar.gz ==> Patching patching file libgearman-1.0/gearman.h ! ›❯ /usr/local/sbin/gearmand -d
  11. $worker = new GearmanWorker(); $worker->addServer(); ! $worker->addFunction("handle", function($job) { //

    get the job data with $job->workload() // mark the job as handled with $job->handle() // return the result }); ! while ($worker->work());
  12. $client = new GearmanClient(); $client->addServer(); ! $client->setCompleteCallback(function($task) { // get

    the result data with $task->data() }); ! $task = $client->addTask("handle", "the task data"); $client->runTasks();
  13. what react is...! ! ›❯ react is a replacement for

    plain old php + apache ›❯ react is like the php equivalent of node js ›❯ react is still beta
  14. what react is not...! ! ›❯ react is not an

    external module ›❯ react is not a framework ›❯ react is not a magic bullet
  15. ›❯ composer require "react/react:0.4.0" ./composer.json has been updated Loading composer

    repositories with package information Updating dependencies (including require-dev) - Installing react/promise (v2.0.0) Loading from cache
  16. $app = function($request, $response) { $response->writeHead(200, [ "Content-Type" => "text/html"

    ]); ! $response->end("hello world"); }; ! $http->on("request", $app);
  17. remember...! ! ›❯ the server is a long-running script ›❯

    unhandled exceptions and fatal errors kill it ›❯ timeouts do not apply ›❯ cli php.ini is used
  18. ›❯ composer require "nikic/fast-route:dev-master" ./composer.json has been updated Loading composer

    repositories with package information Updating dependencies (including require-dev) - Installing nikic/fast-route (dev-master 29c5bf7) Cloning 29c5bf70254d8ce59414646de3e9e43aafc9735e
  19. $app = function($request, $response) use ($dispatcher) { ! $route =

    $dispatcher->dispatch( $request->getMethod(), $request->getPath() );
  20. switch($route[0]) { case FastRoute\Dispatcher::NOT_FOUND: // ... 404 Not Found break;

    ! case FastRoute\Dispatcher::METHOD_NOT_ALLOWED: $allowedMethods = $route[1]; // ... 405 Method Not Allowed break;
  21. ›❯ composer require "cboden/ratchet:0.3.0" ./composer.json has been updated Loading composer

    repositories with package information Updating dependencies (including require-dev) - Installing symfony/routing (v2.4.4) Loading from cache ! - Installing symfony/http-foundation (v2.4.4) Loading from cache
  22. public function onOpen(ConnectionInterface $socket) { $this->socket->send("connected"); } ! public function

    onMessage(ConnectionInterface $socket, $message) { $this->socket->send("message: {$message}"); }
  23. learn more! ! ›❯ composer https://getcomposer.org ›❯ installing react php

    https://github.com/reactphp/react#install ›❯ learning react php http://reactphp.org ›❯ learning ratchet http://socketo.me
  24. something new! ! ›❯ learning new language paradigms is great

    ›❯ event-based code may make cleaner code ›❯ async is useful and is being used in popular frameworks