Asynchronous frameworks allow developers to build stateful protocols and Internet of Things applications without threading and forking. Python, Ruby, and Node.js have had asynchronous frameworks for over ten years. PHP is now starting to catch up with Icicle.io. Learn the basics concepts of event based programming, and how the event loop allows a single thread to process all the requests for your application.
10. S Y S T E M L E V E L A S Y N C
• Gearman
• Apache Spark
• AWS Lambda
• Stomp
• ZeroMQ
• AMQP
J O B S E R V E R S Q U E U I N G
11. C O D E L E V E L A S Y N C
• Forking and
Threading
• Asynchronous I/O
• Fork/Join
• Event Loop
S T R AT E G I E S M E T H O D O L O G I E S
12. F O R K I N G A N D T H R E A D I N G
• Available in PHP core since PHP 4.1
• Requires the use of shared memory
• Requires code to be written for management of
forks/threads
• Creates separate copies of code for each thread/
fork
13. F O R K I N G / T H R E A D I N G L I B R A R I E S
• PCNTL/POSIX — Forking and process management
• pthreads — Thread management
• Semaphore — Semaphore (locks) and shared memory
• Shared Memory — Shared memory
• Sync — Cross platform semaphore and shared
memory
14. A S Y N C H R O N O U S I / O
• Frees up the current process while I/O is
performed
• Executes code based on I/O events
• No copying of code for separate process
15. R E A L W O R L D P R O F I L E D ATA
C AT E G O RY S E G M E N T % T I M E
AV G C A L L S
( P E R T X N )
AV G
T I M E ( M S )
D ATA B A S E M Y S Q L A P P S S E L E C T 7 2 . 6 2 . 0 3 4 . 8
F U N C T I O N
A P I . H A N D L E R S . A P P S : A P P H A N
D L E R . R E N D E R _ R E S O U R C E
1 2 . 6 1 . 0 6 . 0 4
D ATA B A S E M Y S Q L A P P U S E R M A P S E L E C T 8 . 7 3 . 9 7 4 . 1 6
F U N C T I O N
P Y R A M I D . R O U T E R : R O U T E R . _ _
C A L L _ _
3 . 3 1 . 0 1 . 5 9
M Y S Q L O R G A N I Z AT I O N S
S E L E C T
2 . 2 0 . 7 1 . 5 2
16. A S Y N C H R O N O U S I / O L I B R A R I E S
• Streams via stream_select and socket_select
• eio — libeio
• ev — libev
• libevent — libevent
• event — libevent
17. F O R K / T H R E A D V S A S Y N C I / O
• Compute heavy
• Process isolation
• Non-Async library
• I/O processes
• Process/memory
optimization
F O R K / T H R E A D A S Y N C / I O
18. F O R K / J O I N PA R A L L E L I S M
S TA RT
P R O C E S S 1
P R O C E S S 2
P R O C E S S 3
E N DF O R K J O I N
19. F O R K / J O I N E X A M P L E : C U R L
// JOIN
curl_multi_remove_handle($mh, $ch1);
curl_multi_remove_handle($mh, $ch2);
curl_multi_close($mh);
$resp = array(curl_multi_getcontent($ch1),
curl_multi_getcontent($ch2));
// FORK
$active = null;
// Initiate the calls and check for completion
do {
$mrc = curl_multi_exec($mh, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);
while ($active && $mrc == CURLM_OK) {
if (curl_multi_select($mh) != -1) {
do {
$mrc = curl_multi_exec($mh, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);
}
}
// Create both cURL resources and add to cURL Multi
$ch1 = curl_init('http://www.timeapi.org/utc/now');
$ch2 = curl_init('http://www.timeapi.org/utc/now');
$mh = curl_multi_init();
curl_multi_add_handle($mh,$ch1);
curl_multi_add_handle($mh,$ch2);
20. F O R K / J O I N E X A M P L E : G U Z Z L E
// JOIN
$promise1->wait();
$promise2->wait();
// FORK
$handler = function ($response) use (&$times) {
$times[] = $response
->getBody()
->read(512);
};
$promise1 = $client
->getAsync('http://www.timeapi.org/utc/now')
->then($handler);
$promise2 = $client
->getAsync('http://www.timeapi.org/utc/now')
->then($handler);
// Create client, request, and responses array
$client = new GuzzleHttpClient();
$times = array();
21. E V E N T L O O P PA R A L L E L I S M
P R O C E S S
Q U E U E
I T E M
Q U E U E
I T E M
I N
Q U E U E ?
X
Yes
No
A S Y N C
P R O C E S S
A S Y N C
P R O C E S S
A S Y N C
P R O C E S S
A S Y N C
P R O C E S S
22. E V E N T L O O P E X A M P L E
<?php
# Build objects to handle asynchronous interaction
$loop = ReactEventLoopFactory::create();
$dnsFactory = new ReactDnsResolverFactory();
$dns = $dnsFactory->createCached('8.8.8.8', $loop);
$factory = new ReactHttpClientFactory();
$client = $factory->create($loop, $dns);
$resp = array();
# Create callback for handling response
$responseHandler = function ($response) use ($resp) {
$response->on( 'data', function ($data) use ($resp) {
$resp[] = $data;
});
};
# Queue up requests to send
$request = $client->request('GET', 'http://www.timeapi.org/utc/now');
$request->on('response', $responseHandler);
$request->end();
$request = $client->request('GET', 'http://www.timeapi.org/utc/now');
$request->on('response', $responseHandler);
$request->end();
# Run the loop
$loop->run();
23. F O R K / J O I N V S E V E N T L O O P
• Enhance existing
synchronous apps
• Promises
• Hack/HHVM
• Non-HTTP apps
• Full async apps
• High volume apps
F O R K J O I N E V E N T L O O P
25. C A L L B A C K S V S . G E N E R AT O R S
class MyRequestHandler implements RequestHandler {
public function onRequest(Request $request, Socket $socket)
{
$response = new BasicResponse(Response::OK, [
'Content-Type' => 'text/plain',
]);
yield from $response->getBody()->end('Hello, world!');
return $response;
}
public function onError(int $code, Socket $socket)
{
return new BasicResponse($code);
}
}
$server = new Server(new MyRequestHandler());
$server->listen(8080);
Looprun();
$loop = ReactEventLoopFactory::create();
$socket = new ReactSocketServer($loop);
$http = new ReactHttpServer($socket);
$http->on('request', function ($request, $response) {
$response->writeHead(200, [‘Content-Type' => ‘text/plain']);
$response->end("Hello World!n");
});
$socket->listen(8080);
$loop->run();
Callback Generators
31. P H O T O C R E D I T S B Y S L I D E
1: By SteveD. (Flickr) [CC BY 2.0 (http://creativecommons.org/licenses/by/2.0)], via Wikimedia Commons
2: Copyright 2016 by Adam Englander. All rights reserved.
4: By Strober (Own work) [CC BY-SA 3.0 (http://creativecommons.org/licenses/by-sa/3.0)], via Wikimedia Commons
7: By Retro-Computing Society of Rhode Island - Own work, CC BY-SA 3.0, https://commons.wikimedia.org/w/
index.php?curid=5094687
10: By Victorgrigas - Own work, CC BY-SA 3.0, https://commons.wikimedia.org/w/index.php?curid=20348454
14/15: By Justin De La Ornellas from China Town, Hawaii (avex18 Uploaded by clusternote) [CC BY 2.0 (http://
creativecommons.org/licenses/by/2.0)], via Wikimedia Commons
18: By Yoga Balaji - Own work, CC BY-SA 4.0, https://commons.wikimedia.org/w/index.php?curid=35101769
20 Left: User: (WT-shared) Jpatokal at wts wikivoyage [CC BY-SA 3.0 (http://creativecommons.org/licenses/by-sa/
3.0)], via Wikimedia Commons
All others are public domain