Web applications are becoming the norm for users, and being able to handle thousands of requests per second is happening more and more. Developers spend an enormous amount of time making sure that their applications are as fast as possible, but tuning your web server can only go so far. Async Programming is being used by many languages as a quick and easy way to serve web applications, and PHP is no exception. Libraries like ReactPHP and Amp, alongside extensions like Swoole, give developers broad choices for how to build their applications using async principles. See how these tools and async programming can help your application stay quick and agile.
4. Procedural
Programming
4
Do this, then this, then this
$t = new Twitter(new HttpClient());
$tweets = $t->getTweets('dragonmantank');
$template = new Template('tweets.php');
echo $template->render([
‘tweets’ => $tweets,
]);
5. Procedural Programming
• Single Threaded
• Very susceptible to blocking
operations
• Easy to write
5
Make
Twitter
Object
Make
HTTP
Object
Get
Tweets
Read
Template
Render
Template
24. 24
Wait, how does
PHP avoid
blocking?
Web Servers, like Apache,
are multi-threaded
PHP-FPM spins up
multiple threads for use
25. Parallel
Computing
Do this, this, and this at the same
time
$reports = [
new DailyBalance(),
new EndOfDay(),
new ProfitLossUpdate(),
];
foreach ($reports as $r) {
$pid = pcntl_fork();
if ($pid === 0) {
$r->run();
exit();
}
}
25
26. Parallel Programming
• Multiple Threads
• Lets the OS/CPU handle scheduling
• Can be tricky to write
26
Make
Report
Objects
Run Daily
Balance
Report Run End of
Day Report
Run
Profit-Loss
Report
End
89. 89
Most Async is
just Event Loop
Programming
It is a different way to structure
your program
90. 90
Most Async is
just Event Loop
Programming
It is a different way to structure
your program
Async is not:
• Concurrent
• Non-Blocking
• Multi-threaded
• Magic
123. What do Promises provide?
123
• Built off of Promises/A+
• A cleaner interface for callbacks
• A more structured workflow for one logic block to the next
• A way to handle errors in a clean manner
124. A Cleaner
Callback
Interface
use GuzzleHttpPromisePromise;
$fulfilled = function ($value) {
echo 'The promise was fulfilled.';
};
$rejected = function ($reason) {
echo 'The promise was rejected.';
};
$promise = new Promise();
$promise->then($fulfilled, $rejected);
$promise->resolve();
124
125. A Cleaner
Callback
Interface
use GuzzleHttpPromisePromise;
$fulfilled = function ($value) {
echo 'The promise was fulfilled.';
};
$rejected = function ($reason) {
echo 'The promise was rejected.';
};
$promise = new Promise();
$promise->then($fulfilled, $rejected);
$promise->resolve();
125
126. A Cleaner
Callback
Interface
use GuzzleHttpPromisePromise;
$fulfilled = function ($value) {
echo 'The promise was fulfilled.';
};
$rejected = function ($reason) {
echo 'The promise was rejected.';
};
$promise = new Promise();
$promise->then($fulfilled, $rejected);
$promise->resolve();
126
127. A Cleaner
Callback
Interface
use GuzzleHttpPromisePromise;
$fulfilled = function ($value) {
echo 'The promise was fulfilled.';
};
$rejected = function ($reason) {
echo 'The promise was rejected.';
};
$promise = new Promise();
$promise->then($fulfilled, $rejected);
$promise->resolve();
127
128. A Cleaner
Callback
Interface
use GuzzleHttpPromisePromise;
$fulfilled = function ($value) {
echo 'The promise was fulfilled.';
};
$rejected = function ($reason) {
echo 'The promise was rejected.';
};
$promise = new Promise();
$promise->then($fulfilled, $rejected);
$promise->then($fulfilled2, $rejected2);
$promise->resolve();
128