Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Async PHP Redux
Search
Christopher Pitt
May 08, 2014
Technology
2
570
Async PHP Redux
Christopher Pitt
May 08, 2014
Tweet
Share
More Decks by Christopher Pitt
See All by Christopher Pitt
Making Robots (PHP Unicorn Conf)
chrispitt
1
140
Transforming Magento (NomadMage 2017)
chrispitt
2
81
Forget What You Know
chrispitt
1
120
Monads In PHP → php[tek]
chrispitt
3
450
Breaking The Enigma → php[tek]
chrispitt
0
150
Turn on the Generator!
chrispitt
0
150
Implementing Languages (FluentConf)
chrispitt
1
300
Async PHP (Sunshine)
chrispitt
0
400
Helpful Robot
chrispitt
0
94
Other Decks in Technology
See All in Technology
EventHub Startup CTO of the year 2024 ピッチ資料
eventhub
0
110
【Pycon mini 東海 2024】Google Colaboratoryで試すVLM
kazuhitotakahashi
2
500
VideoMamba: State Space Model for Efficient Video Understanding
chou500
0
190
TypeScript、上達の瞬間
sadnessojisan
46
13k
iOS/Androidで同じUI体験をネ イティブで作成する際に気をつ けたい落とし穴
fumiyasac0921
1
110
DMARC 対応の話 - MIXI CTO オフィスアワー #04
bbqallstars
1
160
誰も全体を知らない ~ ロールの垣根を超えて引き上げる開発生産性 / Boosting Development Productivity Across Roles
kakehashi
1
220
B2B SaaSから見た最近のC#/.NETの進化
sansantech
PRO
0
750
テストコード品質を高めるためにMutation Testingライブラリ・Strykerを実戦導入してみた話
ysknsid25
7
2.6k
スクラム成熟度セルフチェックツールを作って得た学びとその活用法
coincheck_recruit
1
140
インフラとバックエンドとフロントエンドをくまなく調べて遅いアプリを早くした件
tubone24
1
430
Application Development WG Intro at AppDeveloperCon
salaboy
0
180
Featured
See All Featured
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
109
49k
Code Review Best Practice
trishagee
64
17k
Mobile First: as difficult as doing things right
swwweet
222
8.9k
A Tale of Four Properties
chriscoyier
156
23k
Building an army of robots
kneath
302
43k
KATA
mclloyd
29
14k
4 Signs Your Business is Dying
shpigford
180
21k
The Illustrated Children's Guide to Kubernetes
chrisshort
48
48k
Docker and Python
trallard
40
3.1k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
4
370
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
28
2k
Designing for humans not robots
tammielis
250
25k
Transcript
async php
disclaimers! ! ›❯ async is not commonplace ›❯ async requires
work ›❯ hatters gonna hat
the problem! ! ›❯ scaling php from the inside is
hard ›❯ persistent connections halt execution ›❯ synchronous core and history
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
asynchronous requests! ! http request hits server → then you
ask for parsed input ! you get parsed input → then you ask for a database read ! ...
fixing the problem! ! ›❯ does it need to be
fixed? ›❯ hardware is cheaper than developers
queues
queue daemons! ! ›❯ beanstalkd ›❯ rabbit mq
queue services! ! ›❯ amazon sqs ›❯ iron mq
›❯ 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
$queue = new Illuminate\Queue\Capsule\Manager(); ! $queue->addConnection([ "driver" => "beanstalkd", "host"
=> "localhost", "queue" => "default" ]); ! $queue->push("SendEmail",["message" => $message]);
class SendEmail { public function fire($job, $data) { // process
the job // mark the job as handled with $job->release() } }
remember...! ! ›❯ queues are one-way ›❯ services have their
quirks
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
gearman
gearman needs...! ! ›❯ a worker script ›❯ a manager
daemon ›❯ a client script
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
›❯ 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
$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());
$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();
learn more! ! ›❯ gearman http://gearman.org ›❯ homebrew http://brew.sh ›❯
docs http://www.php.net/manual/en/book.gearman.php
react php
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
what react is not...! ! ›❯ react is not an
external module ›❯ react is not a framework ›❯ react is not a magic bullet
›❯ 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
require("vendor/autoload.php"); $loop = React\EventLoop\Factory::create(); $socket = new React\Socket\Server($loop); $http =
new React\Http\Server($socket, $loop);
$app = function($request, $response) { $response->writeHead(200, [ "Content-Type" => "text/html"
]); ! $response->end("hello world"); }; ! $http->on("request", $app);
$port = 8080; ! echo "Server at http://127.0.0.1:{$port}"; ! $socket->listen($port);
$loop->run();
›❯ php "index.php" Server at http://127.0.0.1:8080 ! ›❯ curl -X
GET "http://127.0.0.1:8080" hello world
remember...! ! ›❯ the server is a long-running script ›❯
unhandled exceptions and fatal errors kill it ›❯ timeouts do not apply ›❯ cli php.ini is used
routes! ! ›❯ download a router ›❯ define routes ›❯
dispatch requests
›❯ 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
function show($request, $response, $parameters) { $response->end("showing"); } ! function save($request,
$response, $parameters) { $response->end("saving"); }
$dispatcher = FastRoute\simpleDispatcher( function(FastRoute\RouteCollector $collector) { $collector->addRoute("GET", "/products", "show"); $collector->addRoute("POST",
"/products", "save"); } );
$app = function($request, $response) use ($dispatcher) { ! $route =
$dispatcher->dispatch( $request->getMethod(), $request->getPath() );
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;
case FastRoute\Dispatcher::FOUND: $response->writeHead(200, [ "Content-Type" => "text/html" ]); ! call_user_func_array($route[1],
[ $request, $response, $route[2] ]); ! break;
›❯ curl -X GET "http://127.0.0.1:8080/products" showing ! ›❯ curl -X
POST "http://127.0.0.1:8080/products" saving
sockets! ! ›❯ download ratchet ›❯ implement MessageComponentInterface ›❯ pass
your subclass to Http\Server
›❯ 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
use Ratchet\ConnectionInterface; use Ratchet\MessageComponentInterface; ! class Chat extends React\Socket\Server implements
MessageComponentInterface { }
require("vendor/autoload.php"); $loop = React\EventLoop\Factory::create(); // $socket = new React\Socket\Server($loop); $socket
= new Chat($loop); $http = new React\Http\Server($socket, $loop);
var socket = new WebSocket("ws://127.0.0.1:8080"); ! socket.addEventListener("message", function(e) { console.log(JSON.parse(e.data));
}); ! socket.send(JSON.stringify({"foo" : "bar"});
public function onOpen(ConnectionInterface $socket) { $this->socket->send("connected"); } ! public function
onMessage(ConnectionInterface $socket, $message) { $this->socket->send("message: {$message}"); }
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
why bother?
high concurrency! ! ›❯ theoretically... ›❯ higher concurrency != faster
socket programming! ! ›❯ web sockets http://www.html5rocks.com/en/tutorials/websockets/basics ›❯ socket chat
tutorial https://medium.com/laravel-4/eaa550829538
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
thanks! ! ›❯ follow http://twitter.com/followchrisp ›❯ learn http://tutorials.io