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
78
Forget What You Know
chrispitt
1
120
Monads In PHP → php[tek]
chrispitt
3
450
Breaking The Enigma → php[tek]
chrispitt
0
140
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
pandasはPolarsに性能面で追いつき追い越せるのか
vaaaaanquish
4
4.6k
2024-10-30-reInventStandby_StudyGroup_Intro
shinichirokawano
1
630
APIテスト自動化の勘所
yokawasa
7
4.2k
使えそうで使われないCloudHSM
maikamibayashi
0
170
omakaseしないための.rubocop.yml のつくりかた / How to Build Your .rubocop.yml to Avoid Omakase #kaigionrails
linkers_tech
3
740
IaC運用を楽にするためにCDK Pipelinesを導入したけど、思い通りにいかなかった話
smt7174
1
110
ユーザーの購買行動モデリングとその分析 / dsc-purchase-analysis
cyberagentdevelopers
PRO
2
100
ガバメントクラウド先行事業中間報告を読み解く
sugiim
1
1.3k
Forget efficiency – Become more productive without the stress
ufried
0
140
いまさらのStorybook
ikumatadokoro
0
110
【技術書典17】OpenFOAM(自宅で極める流体解析)2次元円柱まわりの流れ
kamakiri1225
0
210
最速最小からはじめるデータプロダクト / Data Product MVP
amaotone
5
740
Featured
See All Featured
A Tale of Four Properties
chriscoyier
156
23k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
37
1.8k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
355
29k
VelocityConf: Rendering Performance Case Studies
addyosmani
325
24k
Unsuck your backbone
ammeep
668
57k
Code Reviewing Like a Champion
maltzj
519
39k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
46
2.1k
The Power of CSS Pseudo Elements
geoffreycrofte
72
5.3k
Building Applications with DynamoDB
mza
90
6.1k
jQuery: Nuts, Bolts and Bling
dougneiner
61
7.5k
Statistics for Hackers
jakevdp
796
220k
Fantastic passwords and where to find them - at NoRuKo
philnash
50
2.8k
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