zend con 2016 - asynchronous prorgamming in php
TRANSCRIPT
![Page 1: Zend con 2016 - Asynchronous Prorgamming in PHP](https://reader031.vdocuments.net/reader031/viewer/2022013004/5883fac31a28ab884b8b4a93/html5/thumbnails/1.jpg)
A S Y N C H R O N O U S P R O G R A M M I N G I N P H P
Z E N D C O N 2 0 1 6
![Page 2: Zend con 2016 - Asynchronous Prorgamming in PHP](https://reader031.vdocuments.net/reader031/viewer/2022013004/5883fac31a28ab884b8b4a93/html5/thumbnails/2.jpg)
W E L C O M E T O L A S V E G A S !
![Page 3: Zend con 2016 - Asynchronous Prorgamming in PHP](https://reader031.vdocuments.net/reader031/viewer/2022013004/5883fac31a28ab884b8b4a93/html5/thumbnails/3.jpg)
W H O A M I ?
![Page 4: Zend con 2016 - Asynchronous Prorgamming in PHP](https://reader031.vdocuments.net/reader031/viewer/2022013004/5883fac31a28ab884b8b4a93/html5/thumbnails/4.jpg)
W H Y A S Y N C H R O N O U S ?
![Page 5: Zend con 2016 - Asynchronous Prorgamming in PHP](https://reader031.vdocuments.net/reader031/viewer/2022013004/5883fac31a28ab884b8b4a93/html5/thumbnails/5.jpg)
H I G H T R A F F I C
![Page 6: Zend con 2016 - Asynchronous Prorgamming in PHP](https://reader031.vdocuments.net/reader031/viewer/2022013004/5883fac31a28ab884b8b4a93/html5/thumbnails/6.jpg)
R E A C T I V E A P P L I C AT I O N S
![Page 7: Zend con 2016 - Asynchronous Prorgamming in PHP](https://reader031.vdocuments.net/reader031/viewer/2022013004/5883fac31a28ab884b8b4a93/html5/thumbnails/7.jpg)
I N T E R N E T O F T H I N G S
![Page 8: Zend con 2016 - Asynchronous Prorgamming in PHP](https://reader031.vdocuments.net/reader031/viewer/2022013004/5883fac31a28ab884b8b4a93/html5/thumbnails/8.jpg)
S TAT E F U L P R O T O C O L S E R V E R S• WebSocket
• MQTT
• AMQP
• IRC
• XMPP
• Telnet
• SSH
• RADIUS
• LDAP
• FTP
![Page 9: Zend con 2016 - Asynchronous Prorgamming in PHP](https://reader031.vdocuments.net/reader031/viewer/2022013004/5883fac31a28ab884b8b4a93/html5/thumbnails/9.jpg)
H T T P / 2
![Page 10: Zend con 2016 - Asynchronous Prorgamming in PHP](https://reader031.vdocuments.net/reader031/viewer/2022013004/5883fac31a28ab884b8b4a93/html5/thumbnails/10.jpg)
H O W D O Y O U D O A S Y N C ?
![Page 11: Zend con 2016 - Asynchronous Prorgamming in PHP](https://reader031.vdocuments.net/reader031/viewer/2022013004/5883fac31a28ab884b8b4a93/html5/thumbnails/11.jpg)
S Y S T E M L E V E L A S Y N C
• German
• Apache Spark
• AWS Lambda
• Stomp
• ZeroMQ
• AMQP
J O B S E R V E R S Q U E U I N G
![Page 12: Zend con 2016 - Asynchronous Prorgamming in PHP](https://reader031.vdocuments.net/reader031/viewer/2022013004/5883fac31a28ab884b8b4a93/html5/thumbnails/12.jpg)
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
![Page 13: Zend con 2016 - Asynchronous Prorgamming in PHP](https://reader031.vdocuments.net/reader031/viewer/2022013004/5883fac31a28ab884b8b4a93/html5/thumbnails/13.jpg)
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
![Page 14: Zend con 2016 - Asynchronous Prorgamming in PHP](https://reader031.vdocuments.net/reader031/viewer/2022013004/5883fac31a28ab884b8b4a93/html5/thumbnails/14.jpg)
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
![Page 15: Zend con 2016 - Asynchronous Prorgamming in PHP](https://reader031.vdocuments.net/reader031/viewer/2022013004/5883fac31a28ab884b8b4a93/html5/thumbnails/15.jpg)
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
![Page 16: Zend con 2016 - Asynchronous Prorgamming in PHP](https://reader031.vdocuments.net/reader031/viewer/2022013004/5883fac31a28ab884b8b4a93/html5/thumbnails/16.jpg)
R E A L W O R L D P R O F I L E D ATAC AT E G O R Y 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 NA P I . H A N D L E R S . A P P S : A P P H A ND 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 NP 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
![Page 17: Zend con 2016 - Asynchronous Prorgamming in PHP](https://reader031.vdocuments.net/reader031/viewer/2022013004/5883fac31a28ab884b8b4a93/html5/thumbnails/17.jpg)
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
![Page 18: Zend con 2016 - Asynchronous Prorgamming in PHP](https://reader031.vdocuments.net/reader031/viewer/2022013004/5883fac31a28ab884b8b4a93/html5/thumbnails/18.jpg)
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
![Page 19: Zend con 2016 - Asynchronous Prorgamming in PHP](https://reader031.vdocuments.net/reader031/viewer/2022013004/5883fac31a28ab884b8b4a93/html5/thumbnails/19.jpg)
F O R K / J O I N PA R A L L E L I S M
S TA R T
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
![Page 20: Zend con 2016 - Asynchronous Prorgamming in PHP](https://reader031.vdocuments.net/reader031/viewer/2022013004/5883fac31a28ab884b8b4a93/html5/thumbnails/20.jpg)
F O R K / J O I N E X A M P L E
// JOINcurl_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 completiondo { $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); } }
<?php// 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);
![Page 21: Zend con 2016 - Asynchronous Prorgamming in PHP](https://reader031.vdocuments.net/reader031/viewer/2022013004/5883fac31a28ab884b8b4a93/html5/thumbnails/21.jpg)
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
![Page 22: Zend con 2016 - Asynchronous Prorgamming in PHP](https://reader031.vdocuments.net/reader031/viewer/2022013004/5883fac31a28ab884b8b4a93/html5/thumbnails/22.jpg)
E V E N T L O O P E X A M P L E<?php# Build objects to handle asynchronous interaction$loop = React\EventLoop\Factory::create(); $dnsFactory = new React\Dns\Resolver\Factory(); $dns = $dnsFactory->createCached('8.8.8.8', $loop); $factory = new React\HttpClient\Factory(); $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();
![Page 23: Zend con 2016 - Asynchronous Prorgamming in PHP](https://reader031.vdocuments.net/reader031/viewer/2022013004/5883fac31a28ab884b8b4a93/html5/thumbnails/23.jpg)
F O R K / J O I N V S E V E N T L O O P
• Enhancing existing traditional synchronous apps
• 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
![Page 24: Zend con 2016 - Asynchronous Prorgamming in PHP](https://reader031.vdocuments.net/reader031/viewer/2022013004/5883fac31a28ab884b8b4a93/html5/thumbnails/24.jpg)
C A L L B A C K S V S . G E N E R AT O R S
![Page 25: Zend con 2016 - Asynchronous Prorgamming in PHP](https://reader031.vdocuments.net/reader031/viewer/2022013004/5883fac31a28ab884b8b4a93/html5/thumbnails/25.jpg)
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); Loop\run();
$loop = React\EventLoop\Factory::create(); $socket = new React\Socket\Server($loop); $http = new React\Http\Server($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
![Page 26: Zend con 2016 - Asynchronous Prorgamming in PHP](https://reader031.vdocuments.net/reader031/viewer/2022013004/5883fac31a28ab884b8b4a93/html5/thumbnails/26.jpg)
W H AT L I E S A H E A D ?
![Page 27: Zend con 2016 - Asynchronous Prorgamming in PHP](https://reader031.vdocuments.net/reader031/viewer/2022013004/5883fac31a28ab884b8b4a93/html5/thumbnails/27.jpg)
R E S O U R C E S
amphp icicle.io ReactPHP
PHP Asynchronous Interoperability Group
RecoilHack/HHVM
![Page 28: Zend con 2016 - Asynchronous Prorgamming in PHP](https://reader031.vdocuments.net/reader031/viewer/2022013004/5883fac31a28ab884b8b4a93/html5/thumbnails/28.jpg)
Q U E S T I O N S ?
![Page 29: Zend con 2016 - Asynchronous Prorgamming in PHP](https://reader031.vdocuments.net/reader031/viewer/2022013004/5883fac31a28ab884b8b4a93/html5/thumbnails/29.jpg)
https://joind.in/19455P L E A S E R A T E T H I S TA L K !
![Page 30: Zend con 2016 - Asynchronous Prorgamming in PHP](https://reader031.vdocuments.net/reader031/viewer/2022013004/5883fac31a28ab884b8b4a93/html5/thumbnails/30.jpg)
P H O T O C R E D I T S B Y S L I D E1: By SteveD. (Flickr) [CC BY 2.0 (http://creativecommons.org/licenses/by/2.0)], via Wikimedia Commons
3: Copyright 2016 by Adam Englander. All rights reserved.
5: By Strober (Own work) [CC BY-SA 3.0 (http://creativecommons.org/licenses/by-sa/3.0)], via Wikimedia Commons
8: By Retro-Computing Society of Rhode Island - Own work, CC BY-SA 3.0, https://commons.wikimedia.org/w/index.php?curid=5094687
11: By Victorgrigas - Own work, CC BY-SA 3.0, https://commons.wikimedia.org/w/index.php?curid=20348454
15/16: 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
19: By Yoga Balaji - Own work, CC BY-SA 4.0, https://commons.wikimedia.org/w/index.php?curid=35101769
21 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