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
410
Helpful Robot
chrispitt
0
94
Other Decks in Technology
See All in Technology
VideoMamba: State Space Model for Efficient Video Understanding
chou500
0
190
Adopting Jetpack Compose in Your Existing Project - GDG DevFest Bangkok 2024
akexorcist
0
110
Zennのパフォーマンスモニタリングでやっていること
ryosukeigarashi
0
160
リンクアンドモチベーション ソフトウェアエンジニア向け紹介資料 / Introduction to Link and Motivation for Software Engineers
lmi
4
300k
心が動くエンジニアリング ── 私が夢中になる理由
16bitidol
0
100
SSMRunbook作成の勘所_20241120
koichiotomo
3
160
個人でもIAM Identity Centerを使おう!(アクセス管理編)
ryder472
4
230
障害対応指揮の意思決定と情報共有における価値観 / Waroom Meetup #2
arthur1
5
490
第1回 国土交通省 データコンペ参加者向け勉強会③- Snowflake x estie編 -
estie
0
130
The Role of Developer Relations in AI Product Success.
giftojabu1
0
140
The Rise of LLMOps
asei
8
1.7k
Flutterによる 効率的なAndroid・iOS・Webアプリケーション開発の事例
recruitengineers
PRO
0
120
Featured
See All Featured
[RailsConf 2023] Rails as a piece of cake
palkan
52
4.9k
Ruby is Unlike a Banana
tanoku
97
11k
Navigating Team Friction
lara
183
14k
Unsuck your backbone
ammeep
668
57k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
PRO
10
720
Designing for humans not robots
tammielis
250
25k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
329
21k
How To Stay Up To Date on Web Technology
chriscoyier
788
250k
The Language of Interfaces
destraynor
154
24k
5 minutes of I Can Smell Your CMS
philhawksworth
202
19k
How to Ace a Technical Interview
jacobian
276
23k
Java REST API Framework Comparison - PWX 2021
mraible
PRO
28
8.2k
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